解题思路
本题可以用最短路算法——Floyd
AC代码
#include<bits/stdc++.h>
#define inf 1e9
using namespace std;
const int N = 2e2 + 50;
int n, m, q, now = 0, a, b, c, t[N], G[N][N];
int main()
{
scanf("%d%d", &n, &m);
for(int i = 0;i<n;i++)
scanf("%d", &t[i]);
for(int i = 0;i<n;i++)
{
for(int j = 0;j<n;j++)
G[i][j] = inf;
G[i][i] = 0;
}
for(int i = 0;i<m;i++)
{
scanf("%d%d%d", &a, &b, &c);
G[a][b] = G[b][a] = c;
}
scanf("%d", &q);
while(q--)
{
scanf("%d%d%d", &a, &b, &c);
while(t[now] <= c && now < n)
{
for(int i = 0;i<n;i++)
for(int j = 0;j<n;j++)
if(G[i][j] > G[i][now] + G[now][j])
G[i][j] = G[j][i] = G[i][now] + G[now][j];
now++;
}
if(t[a] > c || t[b] > c)
puts("-1");
else
{
if(G[a][b] == inf) puts("-1");
else printf("%d\n", G[a][b]);
}
}
return 0;
}