POJ 3255 Roadblocks (Dijkstra求最短路径的变形)(Dijkstra求次短路径)
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 16425 | Accepted: 5797 |
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 4
1 2 100
2 4 200
2 3 250
3 4 100
Sample Output
450
Hint
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <vector>
#include <queue>
using namespace std;
typedef long long ll;
#define MAX_N 5010
#define INF 2147483647 typedef pair<int,int> P; //first是最短距离,second是顶点编号 struct edge{
int to,cost;
}; int n,r; //顶点数,边数
vector <edge> G[MAX_N]; //图的邻接表表示,G[x]表示点x相连的所有的边的集合 int dist1[MAX_N]; //最短路径
int dist2[MAX_N]; //次短路径 void solve(){
//通过指定greater<P>参数,堆按照first从小到大的顺序取出值。
priority_queue<P, vector<P>, greater<P> > que;
fill(dist1, dist1+n, INF);
fill(dist2, dist2+n, INF);
dist1[] = ;
que.push(P(,)); while(!que.empty()){
P p = que.top(); que.pop();
int v = p.second, d = p.first;//取出编号和距离 //如果次短距离比之前加入这个点短,说明加入这个点之后又更新过
//这里continue相当于一种剪枝,没有这句也不会错,但是加了效率会变高
if(dist2[v] < d) continue; //遍历取出的点所连接的所有点
for(int i = ;i < G[v].size(); i++){
edge &e = G[v][i];
int d2 = d + e.cost; //表示当前点可以更新所连接的点的最短路径
if(dist1[e.to] > d2){
swap(dist1[e.to] ,d2); //把之前的距离取出来,更新次短路径
que.push(P(dist1[e.to], e.to)) ; // 把更新过的点加入队列,用这个点去更新其他点
}
//如果d2没有更新过这个点的最短路径,就直接用d2更新次短路径
//如果d2更新过这个点的最短路径,就用d2交换出来的值更新次短路径
if(dist2[e.to] > d2 && dist1[e.to] < d2){
dist2[e.to] = d2;
que.push(P(dist2[e.to], e.to));
}
}
}
printf("%d\n",dist2[n-]);
} int main(){
cin >> n >> r;
int x,y,d;
for(int i = ;i < r; i++){
edge e;
cin >> x >> y >> d;
e.cost = d;e.to = y-;
G[x-].push_back(e);
e.to = x-;
G[y-].push_back(e);
}
solve();
return ;
}
POJ 3255 Roadblocks (Dijkstra求最短路径的变形)(Dijkstra求次短路径)的更多相关文章
- 次最短路径 POJ 3255 Roadblocks
http://poj.org/problem?id=3255 这道题还是有点难度 要对最短路径的算法非常的了解 明晰 那么做适当的修改 就可以 关键之处 次短的路径: 设u 到 v的边权重为cost ...
- POJ 3255 Roadblocks(A*求次短路)
Roadblocks Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 12167 Accepted: 4300 Descr ...
- POJ 3255 Roadblocks (次级短路问题)
解决方案有许多美丽的地方.让我们跳回到到达终点跳回(例如有两点)....无论如何,这不是最短路,但它并不重要.算法能给出正确的结果 思考:而最短的路到同一点例程.spfa先正达恳求一次,求的最短路径的 ...
- POJ 3255 Roadblocks (次短路模板)
Roadblocks http://poj.org/problem?id=3255 Time Limit: 2000MS Memory Limit: 65536K Descriptio ...
- poj 3255 Roadblocks
Roadblocks Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13216 Accepted: 4660 Descripti ...
- poj 3255 Roadblocks 次短路(两次dijksta)
Roadblocks Time Limit : 4000/2000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) Total S ...
- POJ 3255 Roadblocks (次短路 SPFA )
题目链接 Description Bessie has moved to a small farm and sometimes enjoys returning to visit one of her ...
- POJ 3255 Roadblocks (次短路)
题意:给定一个图,求一条1-n的次短路. 析:次短路就是最短路再长一点呗,我们可以和求最短路一样,再多维护一个数组,来记录次短路. 代码如下: #pragma comment(linker, &quo ...
- POJ 3255 Roadblocks --次短路径
由于次短路一定存在,则可知次短路一定是最短路中某一条边不走,然后回到最短路,而且只是一条边,两条边以上不走的话,就一定不会是次短路了(即以边换边才能使最小).所以可以枚举每一条边,算出从起点到这条边起 ...
随机推荐
- Flask-Restful
定义Restful的视图 安装:pip install flask-restful 如果使用Flask-restful,那么定义视图函数的时候,就要继承flask_restful.Resourse类, ...
- BZOJ 2141 分块 线段树
思路: a[i] //By SiriusRen #include <cmath> #include <cstdio> #include <cstring> #inc ...
- Exception异常常见属性
废话少说,直接上代码: try { int n = Convert.ToInt32("@"); } catch(Exception ex) { Console.WriteLine( ...
- 小白向:web中利用request.getPart()上传文件到服务器
被文件上传弄得焦头烂额的一天,果然web中的路径和各种设置真的好讨厌= = 下面是超级小白的.及其简约的“详”解 1.明确目的: 用户将 1.txt 文件 上传到 服务器(web工程下的某个文件夹)中 ...
- 【摘录】JAVA内存管理-自动选择垃圾收集器算法
在J2SE 5.0,垃圾收集的默认值:垃圾收集器.堆大小以及JVM的类型(客户端还是服务器)都会根据应用运行的硬件平台和操作系统自动选择.相比之前设置命令行参数的方式,自动选择很好的匹配了不同类型的应 ...
- 一些特殊ACII码的用法 在控制台中覆盖显示、刷新显示和删除字符
很好奇怎么实现在控制台中不换行直接显示新的信息把旧的替换掉,于是找到了两个ACII码字符,他们可以帮助实现. 一个是‘\b’字符,这个字符是backspace,即删除上一个字符,于是可以清除以显示的旧 ...
- Linux(1)---常用命令
1.将tgz文件解压到指定目录: # tar zxvf test.tgz -C 指定目录 比如将 /lyl/test.tgz解压到 /lyl/linux 目录下 # tar zxvf /lyl/tes ...
- Kendo UI diagram 更改connnect线颜色,及shapes的属性值
1.改diagram中连线的颜色:redraw一下就OK // Change the Line Green diagram.connections[indexS].redraw({ stroke:{ ...
- 微信小程序开发入门(一)
小程序学习入门--(一) 最近自己学习微信小程序的过程当中自己总结出来的知识点,我会不断地更新和完善! 小程序的开发工具 一台电脑 熟悉HTML.CSS.JS基本语法 开发工具: 微信web开发者工 ...
- 数据结构实现(四)二叉查找树java实现
转载 http://www.cnblogs.com/CherishFX/p/4625382.html 二叉查找树的定义: 二叉查找树或者是一颗空树,或者是一颗具有以下特性的非空二叉树: 1. 若左子树 ...