Description
XX star has many cities,
Communication between cities through a strange Highway,
Each piece of SARS imposes a fixed speed limit on the flycar driving,
At the same time, XX stars have special requirements on flycar's "comfort,
That is, the lower the difference between the highest speed and the lowest speed, the more comfortable the ride,
However, XX has less requirements on time.
You need to find the most comfortable path between cities. (SARS is bidirectional ).
Input
Enter multiple test instances, each of which includes:
The first row has two positive integers, n (1 <n <= 200) and M (M <= 1000 ),
It indicates there are n cities and m sars cases.
The following rows are three positive integers: startcity, endcity, speed,
It indicates startcity to endcity on the surface, and the speed limit is speedsars. Speed <= 1000000
Then there is a positive integer Q (q <11), indicating the number of pathfinding.
Next, each row in row Q has two positive integers start and end, indicating the start and end of the path.
Output
Print a line for each route seeking. Only one non-negative integer is output to indicate the maximum comfort speed and the lowest speed of the optimal route.
If the start and end cannot be reached, the output is-1.
Sample Input
4
1 2 2
2 3 4
1 4 1
3 4 2
2
1 3
1 2
Sample output
1
0
Starting from the very beginning, Floyd easily passed the sample results. There were various wa results, and there was no error in finding them. I was reading other people's questions and said "d [I] [J] is the optimal solution." d [I] [k] d [k] [J] is not necessarily the optimal solution, but it is not clear.
Finally, I had to switch to Kruskal.
Sort the links between two points by speed from small to large
Enter the start and end points for each query.
Use the query set to enumerate the speed difference of each China Unicom (starting from the minimum speed, ending from the maximum speed ans = R [I]. W-R [J]. W)
#include<stdio.h>#include<string.h>#include<math.h>#include<iostream>#include<algorithm>#include<queue>#include<stack>#define mem(a,b) memset(a,b,sizeof(a))#define ll __int64#define MAXN 1000#define INF 20000000#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1using namespace std;struct Road{ int u,v,w;};int cmp(Road a,Road b){ return a.w<b.w;}Road r[1000+10];int fat[250],n,m,f,t;int find(int a){ return fat[a]==a?a:find(fat[a]);}void Kru(){ int i,j,k,temp; int minn,maxx; int ans=INF; for(i=1;i<m;i++) { for(k=0;k<=n;k++) fat[k]=k; for(j=i;j<=m;j++) { int x=find(r[j].u); int y=find(r[j].v); if(x!=y) fat[x]=y; if(find(f)==find(t)) { temp=r[j].w-r[i].w; if(temp<ans) ans=temp; break; } } } if(ans==INF) printf("-1\n"); else printf("%d\n",ans);}int main(){ int i,j; int u,v,w,q; while(scanf("%d%d",&n,&m)!=EOF) { for(i=1;i<=m;i++) { scanf("%d%d%d",&r[i].u,&r[i].v,&r[i].w); } sort(r+1,r+1+m,cmp); scanf("%d",&q); while(q--) { scanf("%d%d",&f,&t); Kru(); } } return 0;}
This is the error code, but it is still not understandable:
#include<stdio.h>#include<string.h>#include<math.h>#include<iostream>#include<algorithm>#include<queue>#include<stack>#define mem(a,b) memset(a,b,sizeof(a))#define ll __int64#define MAXN 1000#define INF 20000000#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1using namespace std;struct Road{ int maxx,minn,com;};Road d[230][230];int main(){ int n,m,q,i,j; int u,v,w; int f,t; while(scanf("%d%d",&n,&m)!=EOF) { for(i=0;i<=200;i++) for(j=0;j<=200;j++) { d[i][j].com=INF; d[i][j].maxx=INF; d[i][j].minn=0; } while(m--) { scanf("%d%d%d",&u,&v,&w); d[u][v].maxx=d[v][u].maxx=d[u][v].minn=d[v][u].minn=w; d[u][v].com=d[v][u].com=0; } for(int k=0;k<=n;k++) for(int i=0;i<=n;i++) for(int j=0;j<=n;j++) { int tmax=max(d[i][k].maxx,d[k][j].maxx); int tmin=min(d[i][k].minn,d[k][j].minn); if(d[i][j].com>tmax-tmin) { d[i][j].maxx=tmax; d[i][j].minn=tmin; d[i][j].com=tmax-tmin; } } scanf("%d",&q); while(q--) { scanf("%d%d",&f,&t); if(d[f][t].com!=INF) printf("%d\n",d[f][t].com); else printf("-1\n"); } } return 0;}