POJ 3255 Roadblocks (次短路径 + Dijkstra算法)
来源:互联网 发布:sublime 运行python 编辑:程序博客网 时间:2024/05/16 13:43
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
Source
题意:某条街区共有R条道路,N个路口。道路可以双向通行。问1号路口到N号路口的次短路径长度是多少。次短路径是指所有路径中第二短的路径。并且同一条边可以经过多次。
解析:记最短路径长度为d[ ],次短路径长度为dd[ ],则d[v] = min( d[u] + cost[u][v], dd[u] + cost[u][v] ),所以我们只需要计算出最短路径和次短路径即可。这就跟最短路径不一样了,在实现Dijkstra算法的时候,我们既要记录最短路径,还要记录次短路径。详见代码
PS:第一次写邻接表的形式写最短路,真是学到了!邻接表比邻接矩阵的时间复杂度要好很多,就是写着麻烦一点,不过习惯了就好^_^
AC代码:
#include <cstdio>#include <iostream>#include <algorithm>#include <vector>#include <queue>using namespace std;#define MAX_N 5005#define INF 500000001int N, R;struct edge{ int to, cost; };vector<edge> G[MAX_N]; //无向图的邻接表表示int d[MAX_N], dd[MAX_N]; //d[]最短路,dd[]次短路void solve(){ typedef pair<int, int> P; priority_queue<P, vector<P>, greater<P> > q; //优先级队列优化Dijkstra fill(d, d+N, INF); //跟memset的作用是一样的,初始化数组 fill(dd, dd+N, INF); d[0] = 0; q.push( P(0, 0) ); while(!q.empty()){ P p = q.top(); q.pop(); int v = p.second, d1 = p.first; if(dd[v] < d1) continue; for(int i=0; i<G[v].size(); i++){ edge e = G[v][i]; int d2 = d1 + e.cost; if(d[e.to] > d2){ //更新最短路 swap(d[e.to], d2); q.push( P(d[e.to], e.to) ); } if(dd[e.to] > d2 && d[e.to] < d2){ //更新次短路 dd[e.to] = d2; q.push( P(dd[e.to], e.to) ); } } } printf("%d\n", dd[N-1]);}int main(){ #ifdef sxk freopen("in.txt", "r", stdin); #endif // sxk int to, from, cost; edge foo; while(scanf("%d%d", &N, &R)!=EOF){ for(int i=0; i<R; i++){ scanf("%d%d%d", &from, &to, &cost); from --; to --; //编号换成0~N-1 foo.to = to; foo.cost = cost; G[from].push_back(foo); foo.to = from; //双向边 G[to].push_back(foo); } solve(); } return 0;}
- POJ 3255 Roadblocks (次短路径 + Dijkstra算法)
- POJ, 3255 Roadblocks(次短路径)
- [POJ](3255)Roadblocks---- 最(次)短路径(邻接表+dijkstra优先队列优化)
- POJ 3255 Roadblocks --次短路径
- hdu 6181 Two Paths(次短路径长度)POJ 3255 Roadblocks ( 次短路长度)
- poj3255 Roadblocks (次短路径问题)
- 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 3255 Roadblocks Dijkstra 算法变形
- POJ 3255 Roadblocks (Dijkstra求最短路径的变形)(Dijkstra求次短路径)
- POJ 3255 Roadblocks 次短路问题 (dijjkstra算法)
- POJ 3255 Roadblocks(dijkstra+邻接表求解次短路问题)
- Subversion error: Repository moved permanently to please relocate 解决方法
- Linux Shell 基本语法
- Redis常用命令
- 【SzNOI语法百题】【d028】#号倒三角形
- Permutations II (Java)
- POJ 3255 Roadblocks (次短路径 + Dijkstra算法)
- C++标准库---set应用之通讯录
- MongoDB数据文件备份与恢复
- MySQL在Linux下快速入门
- Android图片滚动,加入自动播放功能,使用自定义属性实现,霸气十足!
- 【SzNOI语法百题】【d029】求出2-100之间的所有质数(素数)
- linux查看java jdk安装路径和设置环境变量
- 由于采用window.location.href进行页面跳转导致的文档已过期
- HTML联动下拉框的内容使用js显示到文本域textarea