find the longest of the shortest HDU - 1595
Marica 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 how many minutes it takes to come from one city to another.
Mirko overheard in the car that one of the roads is under repairs, and that it is blocked, but didn't konw exactly which road. It is possible to come from Marica's city to Mirko's no matter which road is closed.
Marica will travel only by non-blocked roads, and she will travel by shortest route. Mirko wants to know how long will it take for her to get to his city in the worst case, so that he could make sure that his girlfriend is out of town for long enough.Write a program that helps Mirko in finding out what is the longest time in minutes it could take for Marica to come by shortest route by non-blocked roads to his city.InputEach case there are two numbers in the first row, N and M, separated by a single space, the number of towns,and the number of roads between the towns. 1 ≤ N ≤ 1000, 1 ≤ M ≤ N*(N-1)/2. The cities are markedwith numbers from 1 to N, Mirko is located in city 1, and Marica in city N.
In the next M lines are three numbers A, B and V, separated by commas. 1 ≤ A,B ≤ N, 1 ≤ V ≤ 1000.Those numbers mean that there is a two-way road between cities A and B, and that it is 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 Input
5 6 1 2 4 1 3 3 2 3 1 2 4 4 2 5 7 4 5 1 6 7 1 2 1 2 3 4 3 4 4 4 6 4 1 5 5 2 5 2 5 6 5 5 7 1 2 8 1 4 10 2 3 9 2 4 10 2 5 1 3 4 7 3 5 10
Sample Output
11 13 27
题解:找出最短路,然后对路径上的边枚举,更新答案就行,用二维数组容易完成删边的动作,当有几条最短路的时侯,答案就是最短路代表的距离。
1 #include<queue> 2 #include<vector> 3 #include<cstdio> 4 #include<cstring> 5 #include<iostream> 6 #include<algorithm> 7 using namespace std; 8 9 const int INF=1e9+7; 10 const int maxn=1005; 11 12 int n,m; 13 int map[maxn][maxn],d[maxn],Fa[maxn]; 14 bool use[maxn]; 15 16 void DJS(int mark){ 17 memset(use,0,sizeof(use)); 18 for(int i=1;i<=n;i++) d[i]=INF; 19 d[1]=0; 20 21 while(true){ 22 int v=-1; 23 for(int i=1;i<=n;i++) if(!use[i]&&(v==-1||d[i]<d[v])) v=i; 24 if(v==-1) break; 25 use[v]=1; 26 for(int i=1;i<=n;i++){ 27 if(d[i]>d[v]+map[v][i]){ 28 d[i]=d[v]+map[v][i]; 29 if(mark) Fa[i]=v; 30 } 31 } 32 } 33 } 34 35 void Inite(){ 36 for(int i=1;i<=n;i++) Fa[i]=0; 37 for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) map[i][j]=INF; 38 } 39 40 int main() 41 { while(~scanf("%d%d",&n,&m)){ 42 Inite(); 43 for(int i=1;i<=m;i++){ 44 int u,v,w; 45 scanf("%d%d%d",&u,&v,&w); 46 map[u][v]=w; 47 map[v][u]=w; 48 } 49 50 DJS(1); 51 int ans=d[n],tem; 52 for(int i=n;i!=1;i=Fa[i]){ 53 tem=map[i][Fa[i]]; 54 map[i][Fa[i]]=INF; //去掉这条边 55 map[Fa[i]][i]=INF; 56 DJS(0); 57 if(d[n]!=INF) ans=max(ans,d[n]); 58 map[i][Fa[i]]=tem; 59 map[Fa[i]][i]=tem; 60 } 61 printf("%d\n",ans); 62 } 63 return 0; 64 }

浙公网安备 33010602011771号