题目:

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)

Source

 

题解:

  次短路的模板题(注意是严格次短),详细见https://www.cnblogs.com/iiyiyi/p/4706182.html

  但注意代码中的注释部分

代码:

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<ctime>
#include<cctype>
#include<cstring>
#include<string>
#include<algorithm>
#include<queue>
using namespace std;
priority_queue< pair<long long ,int> >que;
const int N=;
const int M=1e5+;
const int inf=0x3f3f3f3f;
int sedis[N],dis[N],fst[N],nxt[M*],go[M*],val[M*],tot,n,m;
inline int R(){
char c;int f=;
for(c=getchar();c<''||c>'';c=getchar());
for(;c<=''&&c>='';c=getchar()) f=(f<<)+(f<<)+c-'';
return f;
}
inline void comb(int a,int b,int c){
nxt[++tot]=fst[a],fst[a]=tot,go[tot]=b,val[tot]=c;
nxt[++tot]=fst[b],fst[b]=tot,go[tot]=a,val[tot]=c;
}
inline void getans(){
memset(dis,inf,sizeof(dis));memset(sedis,inf,sizeof(sedis));
dis[]=;que.push(make_pair(,));
while(!que.empty()){
int u=que.top().second,d=que.top().first;d=-d;que.pop(); //注意这里的d!!!!!!
for(int e=fst[u];e;e=nxt[e]){
int v=go[e];
if(sedis[v]<val[e]+d) continue;
else if(dis[v]>val[e]+d){
sedis[v]=dis[v];
dis[v]=val[e]+d;que.push(make_pair(-dis[v],v));
}
else if(dis[v]<val[e]+d&&sedis[v]>val[e]+d){
sedis[v]=d+val[e];que.push(make_pair(-sedis[v],v));
}
}
}
}
int main(){
//freopen("a.in","r",stdin);
n=R(),m=R();int a,b,c;
for(int i=;i<=m;i++){
a=R(),b=R(),c=R();comb(a,b,c);
}
getans();cout<<sedis[n]<<endl;
return ;
}

算法复习———dijkstra求次短路(poj3255)的更多相关文章

  1. 关于dijkstra求最短路(模板)

    嗯....   dijkstra是求最短路的一种算法(废话,思维含量较低,   并且时间复杂度较为稳定,为O(n^2),   但是注意:!!!!         不能处理边权为负的情况(但SPFA可以 ...

  2. ACM - 最短路 - AcWing 849 Dijkstra求最短路 I

    AcWing 849 Dijkstra求最短路 I 题解 以此题为例介绍一下图论中的最短路算法.先让我们考虑以下问题: 给定一个 \(n\) 个点 \(m\) 条边的有向图(无向图),图中可能存在重边 ...

  3. Aizu-2249 Road Construction(dijkstra求最短路)

    Aizu - 2249 题意:国王本来有一个铺路计划,后来发现太贵了,决定删除计划中的某些边,但是有2个原则,1:所有的城市必须能达到. 2:城市与首都(1号城市)之间的最小距离不能变大. 并且在这2 ...

  4. 850. Dijkstra求最短路 II

    给定一个n个点m条边的有向图,图中可能存在重边和自环,所有边权均为正值. 请你求出1号点到n号点的最短距离,如果无法从1号点走到n号点,则输出-1. 输入格式 第一行包含整数n和m. 接下来m行每行包 ...

  5. 算法复习——floyd求最小环(poj1734)

    题目: 题目描述 N 个景区,任意两个景区之间有一条或多条双向的路来连接,现在 Mr.Zeng 想找一条旅游路线,这个路线从A点出发并且最后回到 A 点,假设经过的路线为 V1,V2,....VK,V ...

  6. 【算法系列学习】Dijkstra求最短路 [kuangbin带你飞]专题四 最短路练习 D - Silver Cow Party

    https://vjudge.net/contest/66569#problem/D trick:1~N各点到X可以通过转置变为X到1~N各点 #include<iostream> #in ...

  7. 849. Dijkstra求最短路 I

    给定一个n个点m条边的有向图,图中可能存在重边和自环,所有边权均为正值. 请你求出1号点到n号点的最短距离,如果无法从1号点走到n号点,则输出-1. 输入格式 第一行包含整数n和m. 接下来m行每行包 ...

  8. POJ-2387(原始dijkstra求最短路)

    Til the Cows Come Home POJ-2387 这题是最简单的最短路求解题,主要就是使用dijkstra算法,时间复杂度是\(O(n^2)\). 需要注意的是,一定要看清楚题目的输入要 ...

  9. Dijkstra求次短路

    #10076.「一本通 3.2 练习 2」Roadblocks:https://loj.ac/problem/10076 解法: 次短路具有一种性质:次短路一定是由起点到点x的最短路 + x到y的距离 ...

随机推荐

  1. mysql架构和历史

    存储引擎 查看: show table status like 'bigcourse'; 结果: +-----------+--------+---------+------------+------ ...

  2. Gulp工具

    Gulp是一个基于node开发的构建工具. gulp本身是不进行任何构建任务,是通过gulp的一些列插件完成: gulp-less  编译LESS文件: gulp-autoprefix  添加css私 ...

  3. 【转摘】TFS上分支和标签的用法

    引用路径:http://blog.csdn.net/cxzhq2002/article/details/8518250 什么时候用分支:  例如为某个客户定制的专用版本,和主干的特性有很大差别.不具通 ...

  4. nginx+php整合(是让nginx可以运行php,以及下载地址)

    下载地址: nginx:http://nginx.org/en/download.html PHP: https://windows.php.net/download/ 都是官网的自己选择版本 安装文 ...

  5. python -- 输出异常详细信息

    在使用try:  except:  捕获异常后,想要获取到异常信息的详细内容另做它用,可以使用python的内置模块traceback进行获取. traceback.print_exc() 直接打印异 ...

  6. Matplotlib 图表的基本参数设置

    1.图名,图例,轴标签,轴边界,轴刻度,轴刻度标签 # 图名,图例,轴标签,轴边界,轴刻度,轴刻度标签等 df = pd.DataFrame(np.random.rand(10,2),columns= ...

  7. POJ:3259-Wormholes(最短路判断负环)

    Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 58153 Accepted: 21747 Descripti ...

  8. 关于我的Android 博客

    我是曹新雨,我为自己代言.现在的菜鸟,3年以后我就是大神.为自己加油.微信:aycaoxinyu 关于我的Android博客,都是我当初遇到困难,克服之后,写上去的.后来,有人加我微信,问我一些问题, ...

  9. Parameter 'limit' not found. Available parameters are [arg1, arg0, pa

    mybatis代码报错,这是因为mapper识别不了limit,需要替换成 LIMIT #{arg0},#{arg1}

  10. runloop和线程有什么关系?

    每条线程都有唯一的一个RunLoop对象与之对应的 主线程的RunLoop是自动创建并启动 子线程的RunLoop需要手动启动 子线程的RunLoop创建步骤如下: 获得RunLoop对象后要调用ru ...