博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ-3255-Roadblocks(次短路的另一种求法)
阅读量:5011 次
发布时间:2019-06-12

本文共 2323 字,大约阅读时间需要 7 分钟。

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: 
A
B, 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 41 2 1002 4 2002 3 2503 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)
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
const int maxn=1e5+5;typedef long long ll;using namespace std;struct node{ int pos,w; node(int x,int y) { pos=x; w=y; } bool friend operator<(node x,node y ) { return x.w>y.w; }};struct edge{ int u,v; ll cost; int nxt;}Edge[maxn<<1];int cnt;ll dis[5005],dis2[5005];int head[5005],vis[5005];void Add(int u,int v,ll w){ Edge[cnt].u=u; Edge[cnt].v=v; Edge[cnt].cost=w; Edge[cnt].nxt=head[u]; head[u]=cnt++;}void Dijkstra(int u){ dis[u]=0; priority_queue
q; q.push(node(u,0)); while(!q.empty()) { node now=q.top(); q.pop(); if(vis[now.pos]) { continue; } vis[now.pos]=1; for(int t=head[now.pos];t!=-1;t=Edge[t].nxt) { if(dis[now.pos]+Edge[t].cost

 

 

转载于:https://www.cnblogs.com/Staceyacm/p/11337315.html

你可能感兴趣的文章
Google Map API V3开发(6) 代码
查看>>
Kafka初入门简单配置与使用
查看>>
第三章Git使用入门
查看>>
Amd,Cmd, Commonjs, ES6 import/export的异同点
查看>>
cocos2dx-Lua与Java通讯机制
查看>>
上下文管理器之__enter__和__exit__
查看>>
android3.2以上切屏禁止onCreate()
查看>>
winform文件迁移工具
查看>>
delphi DCC32命令行方式编译delphi工程源码
查看>>
paip.输入法编程----删除双字词简拼
查看>>
or1200下raw-os学习(任务篇)
查看>>
ZOJ - 3939 The Lucky Week(日期循环节+思维)
查看>>
小花梨的取石子游戏(思维)
查看>>
Ubuntu 18.04安装arm-linux-gcc交叉编译器
查看>>
.net core i上 K8S(一)集群搭建
查看>>
django drf 深入ModelSerializer
查看>>
Android---Menu菜单
查看>>
【资源导航】我所用到过的工具及下载地址
查看>>
监控Tomcat
查看>>
剑指offer编程题Java实现——面试题4后的相关题目
查看>>