Roadblocks
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

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 4
1 2 100
2 4 200
2 3 250
3 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算法求就可以了,我们维护两个数组,d1[],d2[]。
d1保存最短路径,d2保存次短路径
 
我们考虑到,分两种情况:
1.当我们队列中的点没有更新相邻点的最短路径,我们就用它更新次短路径。
2.如果我们队列中的点更新了相邻点的最短路径,我们就用原先的最短路径更新次短路径。
 
 
总而言之:
  就是用所有非形成最短路径的边更新次短路径。
 
Dijkstra算法求单元最短路径:http://www.cnblogs.com/zhangjiuding/p/7712592.html
 
 
 
代码:
#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求次短路径)的更多相关文章

  1. 次最短路径 POJ 3255 Roadblocks

    http://poj.org/problem?id=3255 这道题还是有点难度 要对最短路径的算法非常的了解 明晰 那么做适当的修改 就可以 关键之处 次短的路径: 设u 到 v的边权重为cost ...

  2. POJ 3255 Roadblocks(A*求次短路)

    Roadblocks Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 12167   Accepted: 4300 Descr ...

  3. POJ 3255 Roadblocks (次级短路问题)

    解决方案有许多美丽的地方.让我们跳回到到达终点跳回(例如有两点)....无论如何,这不是最短路,但它并不重要.算法能给出正确的结果 思考:而最短的路到同一点例程.spfa先正达恳求一次,求的最短路径的 ...

  4. POJ 3255 Roadblocks (次短路模板)

    Roadblocks http://poj.org/problem?id=3255 Time Limit: 2000MS   Memory Limit: 65536K       Descriptio ...

  5. poj 3255 Roadblocks

    Roadblocks Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13216 Accepted: 4660 Descripti ...

  6. poj 3255 Roadblocks 次短路(两次dijksta)

    Roadblocks Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) Total S ...

  7. POJ 3255 Roadblocks (次短路 SPFA )

    题目链接 Description Bessie has moved to a small farm and sometimes enjoys returning to visit one of her ...

  8. POJ 3255 Roadblocks (次短路)

    题意:给定一个图,求一条1-n的次短路. 析:次短路就是最短路再长一点呗,我们可以和求最短路一样,再多维护一个数组,来记录次短路. 代码如下: #pragma comment(linker, &quo ...

  9. POJ 3255 Roadblocks --次短路径

    由于次短路一定存在,则可知次短路一定是最短路中某一条边不走,然后回到最短路,而且只是一条边,两条边以上不走的话,就一定不会是次短路了(即以边换边才能使最小).所以可以枚举每一条边,算出从起点到这条边起 ...

随机推荐

  1. Flask上下文管理机制

    前引 在了解flask上下文管理机制之前,先来一波必知必会的知识点. 面向对象双下方法 首先,先来聊一聊面向对象中的一些特殊的双下划线方法,比如__call__.__getattr__系列.__get ...

  2. SVN——Jenkins自动发布

    最近公司项目处于开发阶段,很多功能开发完后就需要发布到测试环境等待测试去验收,这个时候如果手动更新网站的话,是很费时费力的. 于是乎,我们做成了自动发布,这样我们只管提交代码到SVN就行了,发布由软件 ...

  3. 使用JS&jQuery改善用户体验

    第一章  JavaScript基本语法 一.运算符 运算符就是完成操作的一系列符号,它有七类: 赋值运算符(=,+=,-=,*=,/=,%=,<<=,>>=,|=,&= ...

  4. nsrunloop与模式

    scrollview的模式切换:退出原来的模式使用新模式. 2018-04-18 18:16:41.208113+0800 CEMonitor[5014:410604] kCFRunLoopDefau ...

  5. udev的规则文件

    转载于:https://linux.cn/article-9365-1.html 介绍 在 GNU/Linux 系统中,虽然设备的底层支持是在内核层面处理的,但是,它们相关的事件管理是在用户空间中通过 ...

  6. 使用highcharts动态绘制折线图——so easy

    之前学习highcharts发现网上的教程大部分是对highcharts数据的注释,如何动态绘制数据大部分一笔带过,让那些初涉开发的小白云里雾里,所以我就写了一篇这样的博客. <html> ...

  7. .net基础总复习(3)

    第三天 2.单例模式 1)  将构造函数私有化 2)  提供一个静态方法,返回一个对象 3)  创建一个单例 3.XML 可扩展的标记语言 XML:存储数据 注意: XML严格区分大小写,并且成对出现 ...

  8. springcloud关键词解释和基础代码

    原文来自某位大神(不诉薄凉),感觉很好,分享出来. SpringCloud微服务框架搭建 一.微服务架构 1.1什么是分布式 不同模块部署在不同服务器上 作用:分布式解决网站高并发带来问题 1.2什么 ...

  9. 每日Linux命令--不完整命令

    配置文件优化,即把默认的空行还有#注释行去掉,优化前先拷贝一份配置文件 egrep -v '^$|#' 拷贝的配置文件 > 原配置文件 mysql如何修改root用户的密码 方法1: 用SET ...

  10. 如何让myeclipse左边选中文件后自动关联右边树

    在左侧项目树的右上角下拉菜单里有link with editor 点击即可