灾后重建

 原题链接:https://www.luogu.org/problemnew/show/1119#sub

我真傻,真的。我单知道最短路的题目就敲一个spfa,哪曾想这题非floyed不可。

被一道傻逼题耽误这么长时间,也是没谁了。我他妈退役算了。

这是一份参考了题解的奇怪的floyed的代码,慎重食用

 1 #include <iostream>
 2 #include <cstring>
 3 #include <cstdio>
 4 #define maxn 666
 5 using namespace std;
 6 int t[maxn];
 7 int dis[maxn][maxn];
 8 bool vis[maxn];
 9 int m,n,q;
10 const int INF = 0x3f3f3f;
11 inline int read(){
12     int num = 0;
13     char c;
14     bool flag = false;
15     while ((c = getchar()) == ' ' || c == '\n' || c == '\r');
16     if (c == '-')
17         flag = true;
18     else
19         num = c - '0';
20     while (isdigit(c = getchar()))
21         num = num * 10 + c - '0';
22     return (flag ? -1 : 1) * num;
23 }
24 void init(){
25     memset(dis,0x3f,sizeof(dis));
26     for(int i = 1; i <= maxn; i++)
27         dis[i][i] = 0;
28 }
29 int main(){
30     n = read();m = read();
31     for(int i = 1; i <= n; i++)
32         t[i] = read();
33     init();
34     for(int i = 1; i <= m; i++){
35         int u,v,d;
36         u = read();v = read();d = read();
37         dis[++u][++v] = d;
38         dis[v][u] = d;
39     }
40     q = read();
41     while(q--){
42         int x,y,d;
43         x = read();y = read();d =read();
44         x++;y++;
45         for(int k = 1; k <= n; k++){
46             if(vis[k] || t[k] > d)
47                 continue;
48             vis[k] = true;
49             for(int i = 1; i <= n; i++)
50                 for(int j = 1; j <= n; j++)
51                 dis[i][j] = min(dis[i][j],dis[i][k]+dis[k][j]);
52         }
53 
54         if(dis[x][y] == INF || !vis[x] || !vis[y])    
55             printf("-1\n");
56         else
57             printf("%d\n",dis[x][y]);
58     }
59 
60     return 0;
61 }

 

posted @ 2017-10-25 23:27  ShawnZhou_Aether  阅读(166)  评论(0编辑  收藏  举报