P1119 reconstruction
The meaning of problems
There are n m road villages, each village were destroyed by the earthquake, at a time \ (t_i \) is repaired. For a certain route, only when it connects two villages have been repaired to pass. There are a q ask, ask some point shortest path between two villages.
The first line contains two positive integers \ (n-, m \) .
The second line contains \ (n-\) non-negative integers \ (T_0, T_l, ..., N-T_ {}. 1 \) , representing each village complete reconstruction of the time, to ensure that the data \ (t_0 ≤ t_1 ≤ ... ≤ t_ {N-1} \)
Next \ (m \) rows of three non-negative integers \ (I, J, W \) , \ (w≤10000 \) represents the connection there is a village \ (i, j \) in the path length as \ (w \) , to ensure that \ (i \ neq J \) , there is a path for any pair of villages only.
Next, a positive integer \ (q \) , represents \ (q \) th inquiry.
Next \ (Q \) rows of three non-negative integers \ (X, Y, T \) , ask first \ (T \) days, from the village \ (X \) to the village \ (Y \) the shortest path length for the data to ensure that the \ (T \) is not reduced.
Resolve
See the most short-circuit on the first brainless wrote Dij. T found out after the positive solution turned out to be Floyd!
Because the interrogation \ (T \) does not drop, it is repaired in order village, if \ (t_i \ Leq T \) , then the number of 1 ~ i village reconstruction has been completed.
Therefore, Floyd, when the village \ (K \) After completion of reconstruction, with \ (K \) relaxation operation.
Obviously village will be rebuilt once, so complexity is \ (O (n ^ 3) \) it is.
Dij is the \ (O (q \ cdot n \ log_2 (n)) \) is clearly unacceptable.
Code
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
long long n,m,q;
long long t[500];
long long k=1;
long long mape[500][500];
int main() {
scanf("%lld%lld",&n,&m);
for(int i=1;i<=n;++i)
for(int j=1;j<=n;++j)
mape[i][j]=0x7fffffff;
for(int i=1;i<=n;++i) scanf("%lld",&t[i]);
for(int i=1;i<=m;++i) {
long long x,y,z;
scanf("%lld%lld%lld",&x,&y,&z);
mape[x+1][y+1]=z;
mape[y+1][x+1]=z;
}
scanf("%lld",&q);
while(q--) {
long long x,y,z;
scanf("%lld%lld%lld",&x,&y,&z);
++x;++y;
for(;t[k]<=z&&k<=n;++k)
for(int i=1;i<=n;++i)
for(int j=1;j<=n;++j) {
mape[i][j]=min(mape[i][j],mape[i][k]+mape[k][j]);
mape[j][i]=min(mape[j][i],mape[j][k]+mape[k][i]);
}
if(mape[x][y]<0x7fffffff&&t[x]<=z&&t[y]<=z) printf("%d\n",mape[x][y]);
else printf("-1\n");
}
}