首页 > 代码库 > 洛谷灾后重建

洛谷灾后重建

#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int MAXN = 210;const int INF = 1e8;int dis[MAXN][MAXN],t[MAXN];int n,m,k;int main(){    scanf("%d%d",&n,&m);    for (int i=0; i<n; ++i)    //坐标都是从0开始         for (int j=0; j<n; ++j)                dis[i][j] = INF;    for (int i=0; i<n; ++i)        scanf("%d",&t[i]);    //这个是有序的     for (int u,v,w,i=1; i<=m; ++i)    {        scanf("%d%d%d",&u,&v,&w);        dis[u][v] = w;        dis[v][u] = w;    }    t[n] = INF;    //当k到达n时,可以跳出循环     int q,k = 0,u,v,w;    scanf("%d",&q);    while (q--)    {        scanf("%d%d%d",&u,&v,&w);        while (t[k]<=w)    //是t[k]不是k,k表示当前是第几个点修建完成         {            for (int i=0; i<n; ++i)    //Floyd                 for (int j=0; j<n; ++j)                    if (dis[i][k]!=INF&&dis[k][j]!=INF)                        dis[i][j] = min(dis[i][k]+dis[k][j],dis[i][j]);            k++;        }        if (t[u]>w||t[v]>w||dis[u][v]==INF) printf("-1\n");//判断u,v点在w时间是否修建完成,完成是否能到达         else printf("%d\n",dis[u][v]);    }    return 0;}

 

洛谷灾后重建