博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ - 3255-次短路
阅读量:5010 次
发布时间:2019-06-12

本文共 2354 字,大约阅读时间需要 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)
代码:
SPFA;
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define Inf 0x3f3f3f3fconst int maxn=1e5+5;typedef long long ll;using namespace std;struct Edge{ int u,v; int w; int next;}edge[200005];int head[5005],vis[5005],tot;int n,m;int dis[5005][2];void init(){ memset(head,-1,sizeof(head)); memset(vis,0,sizeof(vis)); memset(dis,Inf,sizeof(dis));}void add(int u,int v,int w){ edge[tot].u=u; edge[tot].v=v; edge[tot].w=w; edge[tot].next=head[u]; head[u]=tot++; return;}void SPFA(int s){ dis[s][0]=0; vis[s]=true; queue
q; q.push(s); while(!q.empty()) { int now=q.front(); q.pop(); vis[now]=false; for(int i=head[now];i!=-1;i=edge[i].next) { if(dis[now][0]+edge[i].w
dis[edge[i].v][0]&&dis[now][0]+edge[i].w

 

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

你可能感兴趣的文章
UVALive 4128 Steam Roller 蒸汽式压路机(最短路,变形) WA中。。。。。
查看>>
记忆--1.致我们不可缺少的记忆
查看>>
lintcode28- Search a 2D Matrix- easy
查看>>
react项目
查看>>
C# 万年历 农历 节气 节日 星座 星宿 属相 生肖 闰年月 时辰(转)
查看>>
A Simple Tree Problem
查看>>
Modular Inverse [ZOJ 3609]
查看>>
MySQL性能测试工具之mysqlslap使用详解
查看>>
深入理解jsonp跨域请求原理
查看>>
regsvr32注册COM组件失败
查看>>
jmeter,CSV数据加载、数据库连接、正则
查看>>
(独孤九剑)--正则表达式
查看>>
MySQL学习点滴 --分区表
查看>>
4.6.1 测试基础
查看>>
洛谷 P2486 [SDOI2011]染色
查看>>
oo第三单元总结
查看>>
leetcode : Count and Say [基本功]
查看>>
洛谷 P2485 [SDOI2011]计算器 解题报告
查看>>
c#访问存储过程
查看>>
Slickflow.NET 开源工作流引擎基础介绍(三) -- 基于HTML5/Bootstrap的Web流程设计器
查看>>