Roadblocks
Bessie has moved to a small farm and sometimes enjoys returning to visit one of her best friends. She does not want to get to her old home too quickly, because she likes the scenery along the way. She has decided to take the second-shortest rather than the shortest path. She knows there must be some second-shortest path.
The countryside consists of R (1 ≤ R ≤ 100,000) bidirectional roads, each linking two of the N (1 ≤ N ≤ 5000) intersections, conveniently numbered 1..N. Bessie starts at intersection 1, and her friend (the destination) is at intersection N.
The second-shortest path may share roads with any of the shortest paths, and it may backtrack i.e., use the same road or intersection more than once. The second-shortest path is the shortest path whose length is longer than the shortest path(s) (i.e., if two or more shortest paths exist, the second-shortest path is the one whose length is longer than those but no longer than any other path).
Input
Line 1: Two space-separated integers: N and R
Lines 2..R+1: Each line contains three space-separated integers: A, B, and D that describe a road that connects intersections A and B and has length D (1 ≤ D ≤ 5000)
Output
Line 1: The length of the second shortest path between node 1 and node N
Sample Input
4 4
1 2 100
2 4 200
2 3 250
3 4 100
Sample Output
450
Hint
Two routes: 1 -> 2 -> 4 (length 100+200=300) and 1 -> 2 -> 3 -> 4 (length 100+250+100=450)
Source
题目类型:次短路
算法分析:直接使用Dijkstra算法同时维护最短路和次短路,若能够更新最短路则先更新最短路,然后再更新次短路
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
#pragma comment(linker, "/STACK:102400000,102400000") #include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> using namespace std; #define CFF freopen ("aaa.txt", "r", stdin) #define CPPFF ifstream cin ("aaa.txt") #define DB(ccc) cout << #ccc << " = " << ccc << endl #define PB push_back #define MP(A, B) make_pair(A, B) typedef long long LL; typedef unsigned long long ULL; typedef double DB; typedef pair <int, int> PII; typedef pair <int, bool> PIB; const int INF = 0x7FFFFFFF; const int MOD = 1e9 + 7; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int maxn = 5e3 + 66; vector <PII> edge[maxn]; int n, dis[maxn][2];//n表示节点的个数,dis[i][0]表示从源点到i的最短路,dis[i][1]表示从源点到i的次短路 void Dijkstra (int s) { for (int i = 0; i < maxn; i++) for (int j = 0; j < 2; j++) dis[i][j] = INF; dis[s][0] = 0;//源点到自己的最短路是0,十分重要!!! priority_queue <PII, vector <PII>, greater <PII> > qu; qu.push (PII (0, s)); while (!qu.empty ()) { PII ta = qu.top (); qu.pop (); int v1 = ta.second, d1 = ta.first; if (dis[v1][1] < d1) continue; for (int i = 0; i < edge[v1].size (); i++) { PII tb = edge[v1][i]; int v2 = tb.first, w2 = tb.second; int d2 = d1 + w2; if (dis[v2][0] > d2) { swap (dis[v2][0], d2); qu.push (PII (dis[v2][0], v2)); } if (dis[v2][0] < d2 && dis[v2][1] > d2) { dis[v2][1] = d2; qu.push (PII (dis[v2][1], v2)); } } } } int main() { // CFF; int m; while (scanf ("%d %d", &n, &m) != EOF) { for (int i = 0; i < maxn; i++) edge[i].clear (); for (int i = 1; i <= m; i++) { int u, v, w; scanf ("%d %d %d", &u, &v, &w); edge[u].PB (PII (v, w)); edge[v].PB (PII (u, w)); } Dijkstra (1); printf ("%d\n", dis[n][1]); } return 0; } |
- « 上一篇:poj2393
- poj3723:下一篇 »