POJ3255 Roadblocks

POJ3255 Roadblocks

Description

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

1
2
3
4
5
4 4
1 2 100
2 4 200
2 3 250
3 4 100

Sample Output

1
450

Hint

Two routes: 1 -> 2 -> 4 (length 100+200=300) and 1 -> 2 -> 3 -> 4 (length 100+250+100=450)

Answer

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
#include <cstdio>
#include <utility>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;

const int INF = 1000000;

struct edge {
int to, cost;
};
typedef pair<int, int> P;

int main() {
int N, R;
scanf("%d %d", &N, &R);
vector< vector<edge> > G;
G.resize(N);

for (int i = 0; i < R; ++i) {
int s, t, v;
edge e1, e2;
scanf("%d %d %d", &s, &t, &v);
e1.to = t-1;
e1.cost = v;
e2.to = s-1;
e2.cost = v;
// G[s-1].push_back({t-1, v}); // in c++11
G[s-1].push_back(e1);
G[t-1].push_back(e2);
}

priority_queue<P, vector<P>, greater<P> > que;
int *mind1 = new int[N];
int *mind2 = new int[N];
fill(mind1, mind1+N, INF);
fill(mind2, mind2+N, INF);
mind1[0] = 0;
que.push(P(0, 0));

while (!que.empty()) {
P p = que.top();
que.pop();
int v = p.second, d = p.first;
if (mind2[v] < d) continue;
for (int i = 0; i < G[v].size(); ++i) {
edge e = G[v][i];
int d2 = d + e.cost;
if (mind1[e.to] > d2) {
swap(mind1[e.to], d2);
que.push(P(mind1[e.to], e.to));
}
if (mind2[e.to] > d2 && mind1[e.to] < d2) {
mind2[e.to] = d2;
que.push(P(mind2[e.to], e.to));
}
}
}
printf("%d\n", mind2[N-1]);
return 0;
}

Analysis

  1. 求解最短路径用 Dijkstra 算法。
  2. a 到 b 的次短路径 = min {a到c的最短路径+c到b的次短路径,a到c的次短路径+c到b的最短路径}。
# POJ

Comments

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×