Luo Gu P1119 reconstruction (Floyd)

Topic Portal

Problem-solving ideas:

This is a floyed title. But something different is this question in point is time-limited.

So we do floyed, we can not directly enumerate all transit point, but according to time.

Well, there may be time to ask not repaired two points, we have to judge that.

And in this question, the query t is not falling, so we do not consider the issue of the array. Updated once asked on us, it must be legal.

Of course, because there are inquiries and repair time, so we want to repair after reading.

// thanks simex chiefs of thinking, Friends of the chain there

AC Code:

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 
 5 using namespace std;
 6 
 7 int n,m,g[201][201],t[201],q,k;
 8 bool vis[201];
 9 int x,y,v;
10 
11 int main() {
12     memset(g,0x3f,sizeof(g));
13     scanf("%d%d",&n,&m);
14     for(int i = 0;i < n; i++) 
15         scanf("%d",&t[i]);
16     for(int i = 1;i <= m; i++) {
17         scanf("%d%d%d",&x,&y,&v);
18         g[x][y] = v;g[y][x] = v;
19     }
20     for(int i = 0;i < n; i++)
21         g[i][i] = 0;
22     scanf("%d",&q);
23     for(int a = 1;a <= q; a++) {
24         scanf("%d%d%d",&x,&y,&v);
25         for(k;k < n; k++) {
26             if(t[k] > v) break;
27             else {
28                 for(int i = 0;i < n; i++)
29                     for(int j = 0;j < n; j++) 
30                         g[j][i] = g[i][j] = min(g[i][j],g[i][k] + g[k][j]);
31             }
32         }
33         if(t[x] > v || t[y] > v) {
34             printf("-1\n");
35             continue;
36         }
37         if(g[x][y] >= 0x3f3f3f3f) printf("-1\n");
38         else
39             printf("%d\n",g[x][y]);
40     }
41     return 0;
42 }

 

Guess you like

Origin www.cnblogs.com/lipeiyi520/p/11256709.html