Code:
#include<cstdio> #include<algorithm> using namespace std; const int N=205; const int INF=10000000+233; int d[N][N],times[N]; int main() { //freopen("in.txt","r",stdin); int n,m; scanf("%d%d",&n,&m); for(int i=0;i<n;++i) for(int j=0;j<n;++j) if(i!=j)d[i][j]=d[j][i]=INF; for(int i=0;i<n;++i)scanf("%d",×[i]); times[n]=INF; for(int i=1;i<=m;++i) { int a,b,c; scanf("%d%d%d",&a,&b,&c); d[a][b]=d[b][a]=c; } int q,ed=0,k=0; scanf("%d",&q); while(q--) { int a,b,c; scanf("%d%d%d",&a,&b,&c); while(times[ed+1]<=c)++ed; for(;k<=ed;++k) for(int i=0;i<n;++i) for(int j=0;j<n;++j) if(d[i][k]+d[k][j]<d[i][j])d[i][j]=d[j][i]=(d[i][k]+d[k][j]); if(d[a][b]==INF||a>ed||b>ed)printf("-1 "); else printf("%d ",d[a][b]); } return 0; }