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. 记一次LayUI中Table动态添加列数据

    这次在开发中遇到,有列数不固定的情况.废话不多说,先上图,在上代码. 下面上JS代码 function SearchData() { var dYear = $("#DYear") ...

  2. easyPOI使用

    更多的easyPOI资源的网在easypoi的官网. 1 在pom.xml中添加依赖 <dependency> <groupId>cn.afterturn</groupI ...

  3. 解决SpringBoot项目中Thymeleaf模板的中文乱码问题

    1.使用IDEA创建SpringBoot项目 package com.example.demo; import org.springframework.boot.SpringApplication; ...

  4. 可读流 - nodejs stream总结

    可读流 包含的事件:data,readable,end,close ,error,pause,resume 常用方法:resume,read,pipe,pause 客户端的 HTTP 响应 服务器的 ...

  5. 【Mysql】SpringBoot阿里Druid数据源连接池配置

    一.pom.xml添加 <!-- 配置数据库连接池 --> <dependency> <groupId>com.alibaba</groupId> &l ...

  6. C#LeetCode刷题之#11-盛最多水的容器(Container With Most Water)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3615 访问. 给定 n 个非负整数 a1,a2,...,an,每 ...

  7. NeuralCoref: python的共指消解工具教程

    转载地址 https://blog.csdn.net/blmoistawinde/article/details/81782971 共指消解 首先简要地说说共指消解是什么,有什么用处.假设机器正在阅读 ...

  8. 基于go+etcd实现分布式锁

    原文链接:https://www.yii-china.com/topic/detail/113 package main import ( "context" "fmt& ...

  9. 构造 IPv6 报文

    #!/usr/bin/python from scapy.all import * a=IPv6(nh=58, src='fe80::214:f2ff:fe07:af0', dst='ff02::1' ...

  10. RabbitMQ set password

    问题: -- ::09.387 ERROR oslo.messaging._drivers.impl_rabbit [req-51faf017-4f1f-4a24-ab79-624b302b839b ...