HDU 3986 Harry Potter and the Final Battle
2015-05-03 10:06
302 查看
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, Voldemortis 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 先求出最短路,然后从终点返回删边再算一遍最短路,记下最大值。#include<stdio.h> #include<string.h> #include<vector> #include<queue> #include<functional> using namespace std; const int maxn = 10005; int n, m, x, y, z, dis[maxn], v[5 * maxn], Dis[maxn], ans; struct abc { int next, v; abc(){}; abc(int a, int b) :next(a), v(b){}; }; vector<abc> tree[maxn]; struct cmp { bool operator ()(const int &a, const int &b) { return dis[a] > dis[b]; } }; int dijkstra(int begin, int end, int ex) { priority_queue<int, vector<int>, cmp> p; memset(dis, -1, sizeof(dis)); dis[begin] = 0; p.push(begin); while (!p.empty()) { int q = p.top(); p.pop(); if (q == end) break; for (int i = 0, j; i < tree[q].size(); i++) { j = tree[q][i].next; if (tree[q][i].v != ex) if (dis[j] == -1 || dis[j]> dis[q] + v[tree[q][i].v]) { dis[j] = dis[q] + v[tree[q][i].v]; p.push(j); } } } return dis[end]; } int main() { int T; scanf("%d", &T); while (T--) { scanf("%d%d", &n, &m); for (int i = 0; i <= n; i++) tree[i].clear(); for (int i = 0; i < m; i++) { scanf("%d%d%d", &x, &y, &v[i]); tree[x].push_back(abc(y, i)); tree[y].push_back(abc(x, i)); } ans = dijkstra(1, n, -1); memcpy(Dis, dis, sizeof(dis)); for (int i = n; i != 1;) { for (int j = 0; j < tree[i].size(); j++) { if (Dis[i] == Dis[tree[i][j].next] + v[tree[i][j].v]) { int k = dijkstra(1, n, tree[i][j].v); if (k == -1) ans = -1; else ans = max(ans, k); i = tree[i][j].next; break; } } if (ans < 0) break; } printf("%d\n", ans); } return 0; }
相关文章推荐
- HDU 3986 Harry Potter and the Final Battle 删除一条边的最长最短路
- HDU 3986 Harry Potter and the Final Battle 删掉任意一条边的最长最短路
- hdu 3986 Harry Potter and the Final Battle spfa变形
- hdu 3986 Harry Potter and the Final Battle spfa变形
- I - Harry Potter and the Final Battle HDU - 3986 最短路加重边,dijkstra的邻接表实现
- 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 最短路spfa+枚举
- hdu 3986 Harry Potter and the Final Battle
- hdu 3986 Harry Potter and the Final Battle(最短路+枚举删边)
- hdu 3986 Harry Potter and the Final Battle【最短路SPFA】
- 【HDU】3986 Harry Potter and the Final Battle 最短路
- HDU - 3986 Harry Potter and the Final Battle(最短路SPFA+枚举删边)
- hdu 3986 Harry Potter and the Final Battle (最短路径)
- HDU 3986 Harry Potter and the Final Battle Dijkstra + 堆优化
- hdu 3986 Harry Potter and the Final Battle(最短路变形)
- HDU 3986 Harry Potter and the Final Battle
- hdu 3986 Harry Potter and the Final Battle