HDU1595-最短路-删边
2018-04-14 11:08
197 查看
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
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 HDU 2007-Spring Programming Contest - Warm Up (1) 最近一直1A我有点方啊。。。 题意是某个渣男貌似为了想让女友更晚的回来,要求选择一条路关闭,换言之就是删除一条边之后最短路的最大值,但是不能是INF,我是这么理解的,如果枚举ALL边集显然爆炸,我们可以先跑一遍最短路,记录下最短路上的所有边E,显然删除E之外的边不会影响最短路的长度,所以我们枚举E中的边删除统计最大值。
#include<iostream> #include<cstdio> #include<cstring> #include<queue> #include<algorithm> #include<map> #include<set> #include<vector> #include<functional> using namespace std; #define LL long long #define pii pair<int,int> #define mp make_pair #define inf 0x3f3f3f3f struct Edge{ int u,v,w,next,o; Edge(){} Edge(int u,int v,int w,int next,int o):u(u),v(v),w(w),next(next),o(o){} }e[1002050]; int first[1050],tot; void add(int u,int v,int w){ e[tot]=Edge(u,v,w,first[u],0); first[u]=tot++; } int n,m; int d[1050]; int p[1050]; bool vis[1050]; void dij(){ memset(vis,0,sizeof(vis)); memset(p,-1,sizeof(p)); memset(d,inf,sizeof(d)); d[1]=0; priority_queue<pii,vector<pii>,greater<pii> >q; q.push(mp(0,1)); while(!q.empty()){ int u=q.top().second; q.pop(); if(vis[u]) continue; vis[u]=1; for(int i=first[u];i+1;i=e[i].next){ if(e[i].o==-1) continue; if(d[e[i].v]>d[u]+e[i].w){ d[e[i].v]=d[u]+e[i].w; q.push(mp(d[e[i].v],e[i].v)); p[e[i].v]=i; } } } } int main() { int i,j,k,ans; int u,v,w; while(cin>>n>>m){ memset(first,-1,sizeof(first)); tot=0; while(m--){ scanf("%d%d%d",&u,&v,&w); add(u,v,w); add(v,u,w); } dij(); ans=d ; int c=n; while(c!=1){ e[p[c]].o=1; c=e[p[c]].u; } for(i=0;i<tot;++i){ if(e[i].o==1){ e[i].o=-1; dij(); if(d ==inf) continue; else{ ans=max(ans,d ); } e[i].o=1; } } cout<<ans<<endl; } return 0; }
相关文章推荐
- HDU1595 find the longest of the shortest(最短路)
- HDU1595(枚举+最短路(dijkstra))
- HDU1595 - find the longest of the shortest - 最短路变形+暴力
- hdu1595(枚举+最短路,Dijkstra/SPFA)
- POJ 1062 : 昂贵的聘礼 - 最短路Dijkstra+枚举(难)
- SDUT 2894 C--最短路
- [最短路径] HDU 2544 - 最短路
- [NOIP] [期望DP] [最短路] NOIP2016Day1 换教室
- hdu 2544 最短路 Dijkstra
- hdu 0 or 1(最短路spfa)(本质,抽象)
- hdu 3339 最短路+01背包
- poj2502最短路!
- POJ 3268 Silver Cow Party 最短路
- 【bzoj2834】回家的路 分层图最短路
- 最短路代码资料
- 洛谷 1144 最短路计数
- codeforces 287DIV2 E - Breaking Good(最短路)
- HDU2544 最短路【Dijkstra算法】
- POJ 3259 Wormholes(最短路,判断有没有负环回路)
- BZOJ 1097: [POI2007]旅游景点atr [DP 状压 最短路]