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)
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<vector>
#include<cmath> const int maxn=1e5+;
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<<];
int cnt; ll dis[],dis2[];
int head[],vis[];
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]=;
priority_queue<node>q;
q.push(node(u,));
while(!q.empty())
{
node now=q.top();
q.pop();
if(vis[now.pos])
{
continue;
}
vis[now.pos]=;
for(int t=head[now.pos];t!=-;t=Edge[t].nxt)
{
if(dis[now.pos]+Edge[t].cost<dis[Edge[t].v])
{
dis[Edge[t].v]=dis[now.pos]+Edge[t].cost;
q.push(node(Edge[t].v,dis[Edge[t].v]));
}
}
}
return ;
}
int main()
{
int n,m;
scanf("%d%d",&n,&m);
memset(vis,,sizeof(vis));
memset(head,-,sizeof(head));
memset(dis,0x3f3f3f3f,sizeof(dis));
cnt=;
int u,v;
ll w;
for(int t=;t<m;t++)
{
scanf("%d%d%lld",&u,&v,&w);
Add(u,v,w);
Add(v,u,w);
}
Dijkstra();
for(int t=;t<=n;t++)
{
dis2[t]=dis[t];
}
memset(dis,0x3f3f3f3f,sizeof(dis));
memset(vis,,sizeof(vis));
Dijkstra(n);
int ans=0x3f3f3f3f;
for(int t=;t<cnt;t++)
{
if(dis[Edge[t].u]+dis2[Edge[t].v]+Edge[t].cost<ans&&(dis[Edge[t].u]+dis2[Edge[t].v]+Edge[t].cost)!=dis2[n])
{
ans=dis[Edge[t].u]+dis2[Edge[t].v]+Edge[t].cost;
}
}
printf("%d\n",ans);
system("pause");
return ;
}
												

POJ-3255-Roadblocks(次短路的另一种求法)的更多相关文章

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

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

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

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

  3. POJ 3255 Roadblocks (次短路 SPFA )

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

  4. POJ 3255 Roadblocks (次短路)

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

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

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

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

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

  7. poj - 3225 Roadblocks(次短路)

    http://poj.org/problem?id=3255 bessie 有时会去拜访她的朋友,但是她不想走最快回家的那条路,而是想走一条比最短的路长的次短路. 城镇由R条双向路组成,有N个路口.标 ...

  8. 次最短路径 POJ 3255 Roadblocks

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

  9. poj 3255 Roadblocks

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

  10. POJ 3255 Roadblocks --次短路径

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

随机推荐

  1. Java 通过Jna调用dll路径问题

    调试阶段 C++ dll --> window/system32C# dll --> C:\Program Files\Java\jdk1.8.0_171\bin [jdk bin] 打包 ...

  2. MnasNet:Mobile Neural Architecture Search(MNAS)

  3. XCTF-WEB-高手进阶区(1-4)笔记

    1:baby_web 题目描述:想想初始页面是哪个 通过Dirsearch软件扫描发现Index.php被藏起来了,访问他便会指向1.php 于是通过Burp修改Get为index.php,然后放入R ...

  4. C# winform 弹出窗体给父窗体传值

    Winform程序有很多传值的方法,抱着学习的态度.利用委托注册事件的方法,给窗体统一添加事件: 首先定义一个Frm_Base: namespace 任意 { public partial class ...

  5. MarkDown总结(适合初学者快速入门)

    本文转自https://blog.csdn.net/sun8112133/article/details/79871702 总得的来说,MarkDown是一种简单.轻量级的标记语法,它是基于HTML之 ...

  6. C#LeetCode刷题之#141-环形链表(Linked List Cycle)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3901 访问. 给定一个链表,判断链表中是否有环. 进阶: 你能否 ...

  7. Android popupwindow在低版本手机上无法显示

    popupwindow偶尔的显示失效(在低版本Android系统的手机上,测试机6.0)实在是坑害了不少人,害,而且坑了for a long time.本小白就是其中一个受害者. 百度了N久N多还是没 ...

  8. 关于tomcat的一些基础知识

    tomcat的启动环境是要需要配置jdk的,本次示例用的是jdk1.8和tomcat 8.5. jdk环境变量配置可以在网上随意找到,这里就不再作示范了. 什么是Tomcat Tomcat简单的说就是 ...

  9. 痞子衡嵌入式:解锁i.MXRTxxx上FlexSPI模块自带的地址重映射(Remap)功能

    大家好,我是痞子衡,是正经搞技术的痞子.今天痞子衡给大家介绍的是i.MXRT三位数系列隐藏的FlexSPI Remap功能. 前段时间痞子衡写了一篇文章 <利用i.MXRT1060,1010上新 ...

  10. 第6章 运行Spark SQL CLI

    第6章 运行Spark SQL CLI Spark SQL CLI可以很方便的在本地运行Hive元数据服务以及从命令行执行查询任务.需要注意的是,Spark SQL CLI不能与Thrift JDBC ...