luogu p1119 灾后重建
少用memset(),不然吃亏在后边。
在floyd在如果有些点不能走,等价于它们不能作中转点,这样就满足了时效性,也就满足了在变化的图中求任意两点的最短路
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 #include<iostream> 2 #include<cstdio> 3 #include<queue> 4 #include<algorithm> 5 #include<cmath> 6 #include<ctime> 7 #include<set> 8 #include<map> 9 #include<stack> 10 #include<cstring> 11 #define inf 2147483647 12 #define ls rt<<1 13 #define rs rt<<1|1 14 #define lson ls,nl,mid,l,r 15 #define rson rs,mid+1,nr,l,r 16 #define N 100010 17 #define For(i,a,b) for(long long i=a;i<=b;i++) 18 #define p(a) putchar(a) 19 #define g() getchar() 20 21 using namespace std; 22 23 long long f[310][310]; 24 long long n,m,x,y,c; 25 long long t[500100]; 26 long long k,q; 27 28 void in(long long &x){ 29 long long y=1; 30 char c=g();x=0; 31 while(c<'0'||c>'9'){ 32 if(c=='-')y=-1; 33 c=g(); 34 } 35 while(c<='9'&&c>='0'){ 36 x=(x<<1)+(x<<3)+c-'0';c=g(); 37 } 38 x*=y; 39 } 40 void o(long long x){ 41 if(x<0){ 42 p('-'); 43 x=-x; 44 } 45 if(x>9)o(x/10); 46 p(x%10+'0'); 47 } 48 int main(){ 49 in(n);in(m); 50 For(i,0,n) 51 For(j,0,n) 52 f[i][j]=inf; 53 54 For(i,0,n) 55 t[i]=inf; 56 For(i,0,n-1) 57 in(t[i]); 58 For(i,1,m){ 59 in(x);in(y);in(f[x][y]); 60 f[y][x]=f[x][y]; 61 } 62 For(i,0,n-1) 63 f[i][i]=0; 64 65 in(q); 66 67 For(ii,1,q){ 68 in(x);in(y);in(c); 69 while(t[k]<=c){ 70 For(i,0,n-1) 71 For(j,0,n-1) 72 f[i][j]=min(f[i][j],f[i][k]+f[k][j]); 73 k++; 74 } 75 if(f[x][y]==inf||t[x]>c||t[y]>c) 76 o(-1); 77 else 78 o(f[x][y]); 79 p('\n'); 80 } 81 return 0; 82 }