HDU - 3986 Harry Potter and the Final Battle(最短路SPFA+枚举删边)
2015-06-29 19:17
267 查看
HDU - 3986
Harry Potter and the Final Battle
Submit
Status
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.
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
Sample Output
Source
2011 Multi-University Training Contest 15 - Host by WHU
Harry Potter and the Final Battle
Time Limit: 3000MS | Memory Limit: 65536KB | 64bit IO Format: %I64d & %I64u |
Status
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.
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
3 4 4 1 2 5 2 4 10 1 3 3 3 4 8 3 2 1 2 5 2 3 10 2 2 1 2 1 1 2 2
Sample Output
15 -1 2
Source
2011 Multi-University Training Contest 15 - Host by WHU
题意:
这一道题目和 HDU - 1595 find the longest of the shortest(最短路Dijkstra+枚举删边)
很像。不过这一道的重边要保存起来。并且在枚举边时,若遇到不可达情况,直接 -1 即可。
思路:由于有重边,且重边要保存,故不可以用邻接矩阵保存。用静态链接表,也就是链式前向星。先SPFA求出最短路,保存每个点的前驱点。另外用一个数组保存每条边在 edge 数组中的序号,便于删除。然后枚举删除最短路上的边,每次删除一条边,就求一次最短路,维护 dist
的最大值。链式前向星 edge[] 数组忘记开两倍的边数。。WA了好多次啊!!!
<span style="font-size:18px;">#include <cstdio> #include <iostream> #include <cstring> #include <cmath> #include <string> #include <algorithm> #include <queue> #include <stack> using namespace std; const double PI = acos(-1.0); const double e = 2.718281828459; const double eps = 1e-8; const int MAXN = 1010; const int INF = 1<<29; int dist[MAXN]; int pre[MAXN]; int head[MAXN]; int vis[MAXN]; int edge_id[50010*2]; // 双向边,记得开 2 倍边数 int n, m, edge_cnt; struct Edge { int v; int w; int next; int used; } edge[50010*2]; void add_edge(int u, int v, int w) { edge[edge_cnt].v = v; edge[edge_cnt].w = w; edge[edge_cnt].used = 0; edge[edge_cnt].next = head[u]; head[u] = edge_cnt++; } void SPFA(int f) { int u; memset(vis, 0, sizeof(vis)); for(int i = 1; i <= n; i++) dist[i] = INF; queue<int>Q; Q.push(1); vis[1] = 1; dist[1] = 0; while(!Q.empty()) { int u = Q.front(); Q.pop(); vis[u] = 0; for(int k = head[u]; k != -1; k = edge[k].next) { if(!edge[k].used) { int v = edge[k].v; int w = edge[k].w; if(dist[v] > dist[u]+w) { dist[v] = dist[u]+w; if(f) { pre[v] = u; edge_id[v] = k; } if(!vis[v]) { vis[v] = 1; Q.push(v); } } } } } } int main() { //freopen("in.txt", "r", stdin); //freopen("out.txt", "w", stdout); int Case; cin>>Case; while(Case--) { cin>>n>>m; int u, v, w; edge_cnt = 0; memset(head, -1, sizeof(head)); for(int i = 1; i <= m; i++) { scanf("%d %d %d", &u, &v, &w); add_edge(u, v, w); add_edge(v, u, w); } for(int i = 1; i <= n; i++) pre[i] = 1; SPFA(1); // 1 表示要保存前驱点,0 表示不用 int maxv = dist ; if(maxv >= INF) { printf("-1\n"); continue; } int flag = 1; for(int i = n; i != 1; i = pre[i]) { edge[edge_id[i]].used = 1; SPFA(0); if(dist == INF) { flag = 0; // 遇到不可达情况,直接 -1 break; } if(maxv < dist ) maxv = dist ; edge[edge_id[i]].used = 0; } if(flag) printf("%d\n", maxv); else printf("-1\n"); } return 0; } </span>
相关文章推荐
- IOS--OC--总结
- 30天,App创业从0到1【7.12西安站】
- linux操作系统-两台linux服务器SSH免密码登录
- Exchange Server 2010续定证书系列7-验证证书续定结果
- 黑马程序员——Java基础---IO(一)---IO流概述、字符流、字节流、流操作规律
- 关于Div的宽度与高度的100%设定
- 《C程序员:从校园到职场》出版预告(4):从“散兵游勇”到“正规部队”
- ThinkPHP 1.安装
- 【网络编程笔记】Linux系统常见的网络编程I/O模型简述
- 开源框架的使用细节
- URLconf+MTV:Django眼中的MVC
- Exchange Server 2010续定证书系列6-对其它服务器分配服务
- 作业8.18
- java操作excel文件
- MyBatis-Spring 执行SQL语句的流程
- 动态创建菜单
- 点击按钮实现轮播切换
- 黑马程序员-OC语言-OC多文件开发介绍(听课笔记)
- MP平台--增删改查规则
- redis cluster 搭建