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. 01-java开发环境配置

    1 JDK.JRE.JVM的关系 JDK: java开发工具包 JRE: java运行时环境 JVM: java虚拟机 2 JDK下载 方式一:官网 方式二:该链接提供jdk1.6 ,jdk1.7 , ...

  2. 【av68676164(p41-p42)】内存管理功能

    存储器的功能需求 容量足够大 速度足够快 信息永久保存 多道程序并行 多道程序并行带来的问题 共享:代码和数据共享,节省内存 保护:不允许内存中的程序相互间非法访问 实际存储器体系 三级存储体系 Ca ...

  3. 【工具】之002-Mac下常用工具

    写在前面 我很懒,,,不想敲一个命令一个命令敲... "偷懒是有前提的,不是之前,就是之后." 常用命令 测试端口是否畅通 nc -z 10.254.3.86 30003 查看端口 ...

  4. MyBatisPlus配置日志,CRUD的使用

    配置日志 我们所有的sql在mybatisplus是不可见的,所以在开发中需要配置日志,开发完成后,就可以取消日志了,因为日志也是损耗资源的 #配置日志 mybatis-plus: configura ...

  5. C#LeetCode刷题-树状数组

    树状数组篇 # 题名 刷题 通过率 难度 218 天际线问题   32.7% 困难 307 区域和检索 - 数组可修改   42.3% 中等 315 计算右侧小于当前元素的个数   31.9% 困难 ...

  6. mycat数据库集群系列之数据库多实例安装

    mycat数据库集群系列之数据库多实例安装 最近在梳理数据库集群的相关操作,现在花点时间整理一下关于mysql数据库集群的操作总结,恰好你又在看这一块,供一份参考.本次系列终结大概包括以下内容:多数据 ...

  7. C++动态规划

    数塔: #include <iostream> using namespace std; int a[1000][1000]; int main(){ int n; cin>> ...

  8. 微信DLL劫持反弹shell复现

    (该文参考网络他人资料,仅为学习,不许用于非法用途) 一.操作环境 Windows7 :  微信  , Process Explorer(任务管理工具,本实验中用于查找微信程序调用的DLL文件) Ka ...

  9. N叉树的前后序遍历和最大深度

    package NTree; import java.util.ArrayList; import java.util.List; /** * N叉树的前后序遍历和最大深度 */ public cla ...

  10. 解决MySql Access denied for user 'root'@'192.168.1.119' to databse 的问题

    因为ip未授权,在navicat中执行 grant all privileges on *.* to 'root'@'192.168.1.119' identified by 'root' with ...