hdu 1595 find the longest of the shortest(迪杰斯特拉,减去一条边,求最大最短路)

find the longest of the shortest

Time Limit: 1000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2702    Accepted Submission(s): 974


Problem Description
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.
 
Input
Each 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.
 
Output
In 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
 
Author
ailyanlu
 
Source
 
Recommend
8600   |   We have carefully selected several similar problems for you:  1596 1598 1142 1162 1301 
 
枚举所有的路被删除的情况,用迪杰斯特拉求最短路。
 
题意:一个无向图,n个点,m条边,输入m条边的起点,终点,距离,假设减去其中任意一条边,问各种情况下的最短路中最长的是哪条
 
附上代码:
 
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #define M 1005
 5 #define inf 0x3f3f3f3f
 6 using namespace std;
 7 int map[M][M],vis[M],dis[M],link[M];
 8 int n,m;
 9 int max(int a,int b)
10 {
11     return a>b?a:b;
12 }
13 
14 void djstl(int s,int flag) ///最短路遍历,s代表起点,flag作为标记,初始第一次为1,以后为0
15 {
16     int i,j,minn,t;
17     memset(vis,0,sizeof(vis));
18     for(i=1; i<=n; i++)
19         dis[i]=inf;
20     dis[s]=0;
21     for(i=1; i<=n; i++)
22     {
23         minn=inf;
24         for(j=1; j<=n; j++)
25             if(!vis[j]&&dis[j]<minn)
26             {
27                 minn=dis[j];
28                 t=j;
29             }
30         vis[t]=1;
31         for(j=1; j<=n; j++)
32         {
33             if(!vis[j]&&map[t][j]<inf)
34             {
35                 if(dis[j]>dis[t]+map[t][j])
36                 {
37                     dis[j]=dis[t]+map[t][j];
38                     if(flag)   ///这个点一定为一个缩短路程的转折点,去除后才会导致路程变长
39                     {
40                         link[j]=t;
41                     }
42 
43                 }
44             }
45         }
46     }
47     return;
48 }
49 
50 int main()
51 {
52     int i,j;
53     while(~scanf("%d%d",&n,&m))
54     {
55         memset(link,0,sizeof(link));
56         for(i=1; i<=n; i++)
57             for(j=1; j<=n; j++)
58                 if(i==j) map[i][j]=0;
59                 else     map[i][j]=inf;
60         int a,b,c;
61         while(m--)
62         {
63             scanf("%d%d%d",&a,&b,&c);
64             if(map[a][b]>c)
65                 map[a][b]=map[b][a]=c;
66         }
67         djstl(1,1);  ///第一次初始化图,求出每个点的最短路
68         int ans=dis[n];
69         for(i=n; i!=1; i=link[i])
70         {
71             int tem=map[i][link[i]];
72             map[i][link[i]]=map[link[i]][i]=inf; ///遍历去掉每条路的情况
73             djstl(1,0);
74             ans=max(ans,dis[n]);
75             map[i][link[i]]=map[link[i]][i]=tem;
76         }
77         printf("%d
",ans);
78     }
79     return 0;
80 }
原文地址:https://www.cnblogs.com/pshw/p/5530827.html