POJ 3255 Roadblocks

来源:互联网 发布:linux安装telnet 编辑:程序博客网 时间:2024/06/13 21:16

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: AB, 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 41 2 1002 4 2002 3 2503 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)

求严格次短路,在dijkstra算法的基础上加一些条件搞定

#include<set>#include<map>#include<ctime>#include<cmath>#include<stack>#include<queue>#include<bitset>#include<cstdio>#include<string>#include<cstring>#include<iostream>#include<algorithm>#include<functional>#define rep(i,j,k) for (int i = j; i <= k; i++)#define per(i,j,k) for (int i = j; i >= k; i--)#define lson x << 1, l, mid#define rson x << 1 | 1, mid + 1, rusing namespace std;typedef long long LL;const int low(int x) { return x&-x; }const double eps = 1e-8;const int INF = 0x7FFFFFFF / 3;const int mod = 1e9 + 7;const int N = 2e5 + 10;const int read() {char ch = getchar();while (ch<'0' || ch>'9') ch = getchar();int x = ch - '0';while ((ch = getchar()) >= '0'&&ch <= '9') x = x * 10 + ch - '0';return x;}int T, n, m, x, y, z;int d[N][2], f[N][2];int ft[N], nt[N], u[N], v[N], sz;int dis[4];void merge(int x, int y, int z){rep(i, 0, 1) dis[i] = d[x][i];rep(i, 0, 1) dis[i + 2] = d[y][i] + z;sort(dis, dis + 4);unique(dis, dis + 4);rep(i, 0, 1) d[x][i] = dis[i];}int main(){while (scanf("%d%d", &n, &m) != EOF){sz = 0;rep(i, 1, n){f[i][0] = f[i][1] = 0;ft[i] = -1;d[i][0] = d[i][1] = INF;}rep(i, 1, m){scanf("%d%d%d", &x, &y, &z);u[sz] = y;v[sz] = z;nt[sz] = ft[x];ft[x] = sz++;u[sz] = x;v[sz] = z;nt[sz] = ft[y];ft[y] = sz++;}d[1][0] = 0;while (true){x = y = 0;rep(i, 1, n){if (!f[i][0] && d[i][0] != INF){if (!x || d[x][y] > d[i][0]) x = i, y = 0;}if (!f[i][1] && d[i][1] != INF){if (!x || d[x][y] > d[i][1]) x = i, y = 1;}}if (!x) break;f[x][y] = 1;for (int i = ft[x]; i != -1; i = nt[i]) merge(u[i], x, v[i]);}printf("%d\n", d[n][1]);}return 0;}


0 0
原创粉丝点击