HDU3986Harry Potter and the Final Battle(SPFA删边)
来源:互联网 发布:建筑设计软件试题 编辑:程序博客网 时间:2024/05/17 02:20
Harry Potter and the Final Battle
Time Limit: 5000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 2666 Accepted Submission(s): 761
Problem Description
The final battle is coming. Now Harry Potter is located at city 1, and Voldemort is located at city n. To make the world peace as soon as possible, Of course, Harry Potter will choose the shortest road between city 1 and city n. But unfortunately, Voldemort is so powerful that he can choose to destroy any one of the existing roads as he wish, but he can only destroy one. Now given the roads between cities, you are to give the shortest time that Harry Potter can reach city n and begin the battle in the worst case.
Input
First line, case number t (t<=20).
Then for each case: an integer n (2<=n<=1000) means the number of city in the magical world, the cities are numbered from 1 to n. Then an integer m means the roads in the magical world, m (0< m <=50000). Following m lines, each line with three integer u, v, w (u != v,1 <=u, v<=n, 1<=w <1000), separated by a single space. It means there is a bidirectional road between u and v with the cost of time w.There may be multiple roads between two cities.
Then for each case: an integer n (2<=n<=1000) means the number of city in the magical world, the cities are numbered from 1 to n. Then an integer m means the roads in the magical world, m (0< m <=50000). Following m lines, each line with three integer u, v, w (u != v,1 <=u, v<=n, 1<=w <1000), separated by a single space. It means there is a bidirectional road between u and v with the cost of time w.There may be multiple roads between two cities.
Output
Each case per line: the shortest time to reach city n in the worst case. If it is impossible to reach city n in the worst case, output “-1”.
Sample Input
3441 2 52 4 101 3 33 4 8321 2 52 3 10221 2 11 2 2
Sample Output
15-12
Author
tender@WHU
Source
2011 Multi-University Training Contest 15 - Host by WHU
解题:先用spfa求一条最短路,并记下该边的标记。接下来就是枚举一条一条边(记下的边)删,每次只能删一条边,当遇到一条必须边时就可以直接输出 -1,因为从1不可到达n。如果总是有一条路,那么就输出最大的那条路的花费。
#include<stdio.h>#include<queue>#include<algorithm>#include<vector>using namespace std;const int N = 1005;const int inf = 999999999;struct EDG{ int v,d,id;};vector<EDG>mapt[N];bool inq[N];int dis[N],frome[N],id[N],n;void init(){ for(int i=0;i<=n;i++) { mapt[i].clear(); id[i]=inf; frome[i]=i; }}inline void spfa(int a,bool recode){ queue<int>q; int s; for(int i=1;i<=n;i++) dis[i]=inf,inq[i]=false; inq[n]=true; dis[1]=0; q.push(1); while(!q.empty()) { s=q.front(); q.pop(); inq[s]=false; for(int i=0;i<mapt[s].size();i++) if(id[a]!=mapt[s][i].id||recode) { int now=mapt[s][i].v; if(dis[now]>dis[s]+mapt[s][i].d) { dis[now]=dis[s]+mapt[s][i].d; if(recode) frome[now]=s,id[now]=mapt[s][i].id; if(!inq[now]) inq[now]=true,q.push(now); } } }}int main(){ int m,a,b,ans,t; EDG edg; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m); init(); while(m--) { scanf("%d%d%d",&a,&b,&edg.d); edg.id=m; edg.v=b; mapt[a].push_back(edg); edg.v=a; mapt[b].push_back(edg); } if(n==1) { printf("0\n");continue; } spfa(0,true); ans=-1; a=n; while(frome[a]!=a) { b=frome[a]; spfa(a,false); if(dis[n]>ans&&dis[n]!=inf) ans=dis[n]; else if(dis[n]==inf) { ans=-1; break; } a=b; } printf("%d\n",ans); }}
0 0
- HDU3986Harry Potter and the Final Battle(SPFA删边)
- hdu3986Harry Potter and the Final Battle
- hdu3986Harry Potter and the Final Battle(最短路+枚举)
- hdu 3986 Harry Potter and the Final Battle spfa变形
- hdu3986 Harry Potter and the Final Battle
- HDU3986(Harry Potter and the Final Battle)-枚举最短路径+SPFA
- HDU - 3986 Harry Potter and the Final Battle(最短路SPFA+枚举删边)
- hdu 3986 Harry Potter and the Final Battle 最短路spfa+枚举
- hdu 3986 Harry Potter and the Final Battle【最短路SPFA】
- HDU3986 Harry Potter and the Final Battle最短路
- hdu 3986 Harry Potter and the Final Battle
- HDU 3986 Harry Potter and the Final Battle
- hdoj 3986 Harry Potter and the Final Battle
- hdu 3986 Harry Potter and the Final Battle
- hdu 3986 Harry Potter and the Final Battle
- HDU 3986 Harry Potter and the Final Battle
- hdu 3986 Harry Potter and the Final Battle
- HDU 3986 Harry Potter and the Final Battle(Dijkstra)
- mongodb系列之一:java操作mongodb数据库
- 第五周 项目2-对象作为数据成员
- java学习(10)IO相关类总结
- mongodb系列之二:连接池
- python数组的使用
- HDU3986Harry Potter and the Final Battle(SPFA删边)
- Awk脚本
- 数据结构之递归
- 类和方法
- sqoop-1.99.3 安装配置
- Swift入门(一)
- JavaScript创建对象的几种方式
- C语言中的atan和atan2
- mongodb系列之三:java分页