HDU1595-最短路-删边

find the longest of the shortest

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


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
    最近一直1A我有点方啊。。。
   题意是某个渣男貌似为了想让女友更晚的回来,要求选择一条路关闭,换言之就是删除一条边之后最短路的最大值,但是不能是INF,我是这么理解的,如果枚举ALL边集显然爆炸,我们可以先跑一遍最短路,记录下最短路上的所有边E,显然删除E之外的边不会影响最短路的长度,所以我们枚举E中的边删除统计最大值。
   
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<queue>
 5 #include<algorithm>
 6 #include<map>
 7 #include<set>
 8 #include<vector>
 9 #include<functional>
10 using namespace std;
11 #define LL long long 
12 #define pii pair<int,int>
13 #define mp make_pair
14 #define inf 0x3f3f3f3f
15 struct Edge{
16     int u,v,w,next,o;
17     Edge(){}
18     Edge(int u,int v,int w,int next,int o):u(u),v(v),w(w),next(next),o(o){}
19 }e[1002050];
20 int first[1050],tot;
21 void add(int u,int v,int w){
22     e[tot]=Edge(u,v,w,first[u],0);
23     first[u]=tot++;
24 }
25 int n,m;
26 int d[1050];
27 int p[1050];
28 bool vis[1050];
29 void dij(){
30     memset(vis,0,sizeof(vis));
31     memset(p,-1,sizeof(p));
32     memset(d,inf,sizeof(d));
33     d[1]=0;
34     priority_queue<pii,vector<pii>,greater<pii> >q;
35     q.push(mp(0,1));
36     while(!q.empty()){
37         int u=q.top().second;
38         q.pop();
39         if(vis[u]) continue;
40         vis[u]=1;
41         for(int i=first[u];i+1;i=e[i].next){
42             if(e[i].o==-1) continue;
43             if(d[e[i].v]>d[u]+e[i].w){
44                 d[e[i].v]=d[u]+e[i].w;
45                 q.push(mp(d[e[i].v],e[i].v));
46                 p[e[i].v]=i;
47             }
48         }
49     }
50 }
51 int main()
52 {
53     int i,j,k,ans;
54     int u,v,w;
55     while(cin>>n>>m){
56         memset(first,-1,sizeof(first));
57         tot=0;
58         while(m--){
59             scanf("%d%d%d",&u,&v,&w);
60             add(u,v,w);
61             add(v,u,w);
62         }
63         dij();
64         ans=d[n];
65         int c=n;
66         while(c!=1){
67             e[p[c]].o=1;
68             c=e[p[c]].u;
69         }
70         for(i=0;i<tot;++i){
71             if(e[i].o==1){
72                 e[i].o=-1;
73                 dij();
74                 if(d[n]==inf) continue;
75                 else{
76                     ans=max(ans,d[n]);
77                 }
78                 e[i].o=1;
79             }
80         }
81         cout<<ans<<endl;
82     }
83     return 0;
84 }
原文地址:https://www.cnblogs.com/zzqc/p/8830900.html