/* -- Defined Functions -- */ #define For(a,x,y) for (int a = x; a <= y; ++a) #define Forw(a,x,y) for (int a = x; a < y; ++a) #define Bak(a,y,x) for (int a = y; a >= x; --a)
namespace FastIO {
inlineintgetint(){ int s = 0, x = 1; char ch = getchar(); while (!isdigit(ch)) { if (ch == '-') x = -1; ch = getchar(); } while (isdigit(ch)) { s = s * 10 + ch - '0'; ch = getchar(); } return s * x; } inlinevoid __basic_putint(int x) { if (x < 0) { x = -x; putchar('-'); } if (x >= 10) __basic_putint(x / 10); putchar(x % 10 + '0'); }
signedmain(){ #define HANDWER_FILE #ifndef HANDWER_FILE freopen("testdata.in", "r", stdin); freopen("testdata.out", "w", stdout); #endif usingnamespace Solution; using FastIO::getint; n = getint(); m = getint(); memset(ttime, 0x3f, sizeof ttime); memset(dis, 0x3f, sizeof dis); for (int i = 0; i < n; ++i) { ttime[i] = getint(); } for (int i = 1; i <= m; ++i) { int prev = getint(); int next = getint(); int weight = getint(); dis[prev][next] = dis[next][prev] = weight; } for (int i = 0; i <= n; ++i) dis[i][i] = 0; q = getint(); for (int i = 1; i <= q; ++i) { int s = getint(); int t = getint(); int timee = getint(); int ans = 0; while (ttime[k] <= timee) { for (int a = 0; a < n; ++a) { for (int b = 0; b < n; ++b) { dis[a][b] = std::min(dis[a][b], dis[a][k] + dis[k][b]); } } ++k; } if (dis[s][t] == 0x3f3f3f3f || ttime[s] > timee || ttime[t] > timee) ans = -1; else ans = dis[s][t]; printf("%d\n", ans); } return0; }