Find the longest of the shortest
Time limit:1000/5000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 2094 Accepted Submission (s): 739
Problem Descriptionmarica is very angry with Mirko because he found a new girlfriend and she seeks revenge. Since She doesn ' t live in the same city, she started preparing for the long journey. We know for every road what many minutes it takes to come from one city to another.
Mirko overheard in the car that one of the roads are under repairs, and that it's blocked, but didn ' t konw exactly which R Oad. It is possible to come from Marica's city to Mirko's no matter which road is closed.
Marica'll travel is only by non-blocked roads, and she'll travel by shortest route. Mirko wants to know how long would it take for her and get to the the worst case, so, he could make sure that hi s girlfriend is out of the town for long enough. Write A program This helps Mirko in finding out what's the longest time in minutes it could take for Marica to come by sh Ortest route by Non-blocked roads.
Inputeach case there is both numbers in the first row, N and M, separated by a single space, the number of Towns,and the N Umber of roads between the towns. 1≤n≤1000, 1≤m≤n* (N-1)/2. The cities was markedwith numbers from 1 to N, Mirko was located in City 1, and Marica in City N.
In the next M lines is three numbers A, B and V, separated by commas. 1≤a,b≤n, 1≤v≤1000.those numbers mean that there was a two-way road between cities A and B, and that it's crossable In V minutes.
Outputin the first line of the output file write the maximum time in minutes, it could take Marica to come to Mirko.
Sample INPUT5 61 2 41 3 32 3 12 4 42 5 74 5 16 71 2 12 3 43 4 44 6 41 5 52 5 25 6 55 71 2 81 4 102 3 92 4 102 5 13 4 73 5 10
Sample Output111327 Test Instructions: First give you two number n,m, indicating that there are N cities M Road, followed by M-line, three numbers per row for the starting point and time spent, note that the path is bidirectional. And then you ask for the shortest path, but the M-Path eventually has a way to go, so you ask for the worst case of the shortest path. Idea: Consider the worst case of the shortest path, that can not go that road must be in the shortest path above, all you want to find the shortest path, and then try to remove the shortest path of any road, and then find a short path, and finally take the minimum path maximum is the worst case of the shortest path, The Dijkstra algorithm is used in this code to find the shortest path. AC Code:
#include <iostream>#include<cstdio>#include<climits>#include<cstring>using namespacestd;introad[1005][1005];intn,m;intfa[1005];intdist[1005];intvis[1005];intsum;intMin (intAintb) { returnA<b?a:b;}intMax (intAintb) { returnA>b?a:b;}intDijkintStartintLast ) { for(intI=1; i<=n;i++) {Dist[i]=Int_max; } Dist[start]=0; memset (Vis,0,sizeof(VIS)); for(intj=0; j<n;j++) { intm=int_max,x; for(intI=1; i<=n;i++) { if(!vis[i]&&dist[i]<m) {m=Dist[i]; X=i; }} Vis[x]=1; for(intI=1; i<=n;i++) { if(road[x][i]!=-1) {Dist[i]=min (dist[i],dist[x]+Road[x][i]); } } } returndist[last];}intMain () { while(~SCANF ("%d%d",&n,&m) {memset (road,-1,sizeof(road)); for(intI=0; i<m;i++) { intstart,last,temp; scanf ("%d%d%d",&start,&last,&temp); Road[start][last]=road[last][start]=temp; } intMaxn=dijk (1, N); Sum=0; intx=N; Fa[sum++]=x; while(x!=1) { for(intI=1; i<=n;i++) { if(dist[i]==int_max| | road[i][x]==-1)Continue; if(dist[x]==dist[i]+Road[i][x]) {Fa[sum++]=i; X=i; Break; } } } for(intI=1; i<sum;i++) { ints=Fa[i]; intt=fa[i-1]; inttemp=Road[s][t]; Road[s][t]=road[t][s]=-1; MAXN=max (Maxn,dijk (1, N)); Road[s][t]=road[t][s]=temp; } printf ("%d\n", MAXN); } return 0;}
HDU 1595 Shortest Path algorithm Dijkstra