题目:

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. IDEA的下载安装和激活

    1.下载网站http://www.jetbrains.com/ 2. 3. 4.注意要下载Ultimate版本 5.安装 下载完成后直接下一步,傻瓜式安装 6.激活,在图片位置输入激活码即可 !!!! ...

  2. Lucene简单总结

    Lucene API Document Document:文档对象,是一条原始数据 文档编号 文档内容 1 谷歌地图之父跳槽FaceBook 2 谷歌地图之父加盟FaceBook 3 谷歌地图创始人拉 ...

  3. Lo、Hi、HiByte、LoWord、HiWord、MakeWord、MakeLong、Int64Rec

    本话题会涉及到: Lo.Hi.HiByte.LoWord.HiWord.MakeWord.MakeLong.Int64Rec 譬如有一个 Cardinal 类型的整数: 1144201745其十六进制 ...

  4. TouTiao开源项目 分析笔记6

    1.NewsChannelBean简单类笔记 1.1.Comparable接口的实现和使用 参考文章:Comparable接口的实现和使用. 因为NewsChannelBean实现了Comparabl ...

  5. python字典的整理信息

    字典的增删改查大纲 增: dic={'age':18,'name':'liu','sex':'male'} dic['high'] = 185 #没有键值对,添加 dic['age'] = 16 #有 ...

  6. 微信小程序 | 51,live新课“小程序UI容器组件”的课堂计划

    零基础前端自学入门:小程序UI容器组件 这是一节以UI布局.容器组件的使用为主题的live,专注于布局与容器这一个点,努力把这一点讲透.这是继4月22日整体入门live“零基础周末学习小程序开发”之后 ...

  7. anr trace文件分析

    测试给的trace文件好几万行,怎么看? 1.搜索 你的包名,看它报错误报在你代码的哪里 2.在你代码里面分析 还有,synchronized 就是用来防止多线程调用的,没有那么神奇.

  8. java 中实体Bean和Map互相转化

    技术交流群: 233513714 // 将一个map对象转化为bean public static void transMap2Bean(Map<String, Object> map, ...

  9. loj2174 「FJOI2016」神秘数

    先考虑一下一个集合怎么用 \(O(n)\) 时间求出来,然后用主席树推广到一个序列就可以了.大致思想就是考虑一个数的权值和它前面的数的和的关系. #include <algorithm> ...

  10. OVGap 原生与JS交互

    源代码:https://github.com/windshg/OVGap OVGap:一个轻量级的类库,能够让iOS应用和远程网页的 Javascript 代码进行通信,也就是说,远程的 Javasc ...