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
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
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;}
- poj 3255 Roadblocks
- poj 3255 Roadblocks
- POJ-3255-Roadblocks
- POJ 3255 Roadblocks
- POJ 3255 Roadblocks
- poj 3255 Roadblocks
- POJ 3255 Roadblocks
- poj 3255 Roadblocks
- POJ 3255 Roadblocks
- POJ 3255 Roadblocks
- POJ-3255-Roadblocks
- poj 3255 Roadblocks
- poj 3255 Roadblocks
- POJ 3255 Roadblocks
- POJ 3255 Roadblocks
- POJ-3255 Roadblocks
- poj 3255 Roadblocks
- POJ 3255 Roadblocks
- NSTimer计时器后台运行的解决方法
- POJ 3111 - K Best(0/1分数规划)
- 设计模式总结之Facade Pattern(外观模式)
- IDEA 单元测试testng入门及testng.xml
- 欢迎使用CSDN-markdown编辑器
- POJ 3255 Roadblocks
- iOS定时刷新按钮的文字,避免按钮闪烁的办法
- SCORM标准课件中SCO的制作方法
- iOS 使用opencv 灰度图 黑白颜色转换
- 一个90后创业者血与泪的教训
- Facial Landmark Detection
- codeblocks 多线程编程时出现:对pthread_create未定义的引用,解决方法
- $,ajxa动态获取数据表格显示
- 安卓中关于侧滑菜单的简单应用