POJ - 3255 Roadblocks(最短路Dijkstra算法求次最短路)
来源:互联网 发布:木床 品牌 知乎 编辑:程序博客网 时间:2024/05/01 14:37
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
题意:
某街道共有R条道路、N个路口。道路可以双向通行。问1号路口到N号路口的次短路长度是多少?同一条边可以经过多次。
分析:
Dijkstra 算法的思路是依次确定尚未确定的顶点中距离最小的顶点,利用这个思路,可以看到:到某个顶点v的次短路要么是其他某个顶点u的最短路再加上u->v这条边(且加上后要求比最短路长),要么就是u的次短路再加上u->v的边。于是我们用Dijkstra的思路可以求出所有顶点的最短路和次短路。
由于边是可以重复经过的,对下面的样例,输出是140,与常识不同。
输入:
4 4
1 2 100
2 4 200
2 3 10
3 4 10
输出:
140
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<functional>#include<queue>#include<vector>using namespace std;const int mnx = 5000 + 100;const int mxe = 100000 + 1000;const int INF = 0X3f;int dis[mnx];int dis2[mnx];int vis[mnx];vector<int>g[mnx],e[mnx];int n, m;struct point{int num;int dis;bool operator < (const point & b)const{return dis>b.dis;}point(int d, int n){num = n;dis = d;}};void add(int v, int u,int c){g[v].push_back(u);e[v].push_back(c);}void dijkstra(int s){memset(dis, INF, sizeof(dis));memset(dis2, INF, sizeof(dis2));priority_queue<point>que;dis[s] = 0;que.push(point(0, 1));while (!que.empty()){point p = que.top(); que.pop();int v = p.num;int d = p.dis;if (dis2[v] < d) continue;for (int i = 0; i < g[v].size(); i++){int c = e[v][i];int u = g[v][i];int d2 = d + c;if (dis[u] > d2){swap(dis[u], d2);que.push(point(dis[u], u));}if (dis2[u] > d2 && dis[u] < d2){dis2[u] = d2;que.push(point(dis2[u], u));}}}}int main(){while (scanf("%d%d", &n, &m) != EOF){int u, v, c;for (int i = 0; i < m; i++){scanf("%d%d%d", &u, &v, &c);add(u, v, c);add(v, u, c);}dijkstra(1);printf("%d\n", dis2[n]);}}
- POJ - 3255 Roadblocks(最短路Dijkstra算法求次最短路)
- POJ 3255 Roadblocks (dijkstra最短路)
- POJ 3255 Roadblocks 最短路Dijkstra+堆优化
- poj 3255 Roadblocks Dijkstra求次短路
- [POJ 3255]Roadblocks[dijkstra][次短路]
- poj 3255 Roadblocks (次短路+dijkstra)
- Roadblocks(次短路-dijkstra算法)
- POJ 3255 Roadblocks (Dijkstra求次短路)
- POJ 3255 RoadBlocks(次短路+dijkstra变形)
- poj-1502(最短路dijkstra算法)
- POJ 3255 Roadblocks 次短路问题 (dijjkstra算法)
- POJ 3255 Roadblocks(dijkstra+邻接表求解次短路问题)
- POJ 3255-Roadblocks [次短路 Dijkstra] 《挑战程序设计竞赛》2.5
- POJ 3255 Roadblocks(次短路,Dijkstra变形+邻接表存储)
- 最短路算法(dijkstra)
- 最短路(Dijkstra算法)
- POJ 题目3255Roadblocks(次短路)
- POJ 3255 Roadblocks(次短路)
- 解析VC++ Name Mangling 机制
- EmguCV使用Stitcher类来拼接图像
- PostGreSQL 结合 Hibernate 在项目中的使用小结
- Android刷新页面
- POJ 1320 Street Numbers 佩尔方程
- POJ - 3255 Roadblocks(最短路Dijkstra算法求次最短路)
- 注入器Spring
- spring的annotation-driven配置事务管理器
- 加密你的SQLite
- nc +tar 传送文件
- JDBC六部曲
- Android取消文本框聚焦事件
- 关于Linux下JDK的安装和卸载,Tomcat的配置。
- android studio 常用插件