Description

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)

Source

 
记最短路径长度为dis[ ],次短路径长度为ddis[ ],则dis[v] = min( dis[u] + cost[u][v],  ddis[u] + cost[u][v] ),所以我们只需要计算出最短路径和次短路径即可。这就跟最短路径不一样了,在实现Dijkstra算法的时候,我们既要记录最短路径,还要记录次短路径。
 
 
代码:
 #include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <string>
#include <cstring>
#include <queue>
using namespace std;
#define INF 0x3f3f3f3f
#define MAX 5055 int N,R;
struct node{
int to;
int cost;
};
vector<node> g[MAX];
int dis[MAX],ddis[MAX]; void solve()
{
typedef pair<int,int> P;
priority_queue<P,vector<P>,greater<P> > que;
fill(dis,dis+N,INF);
fill(ddis,ddis+N,INF);
dis[]=;
que.push(P(,)); while(!que.empty()){
P p=que.top(); que.pop();
int v=p.second,d1=p.first;
if(ddis[v]<d1) continue;
for(int i=; i<g[v].size(); i++){
node s=g[v][i];
int d2=d1+s.cost;
if(dis[s.to]>d2){
swap(dis[s.to],d2);
que.push(P(dis[s.to],s.to));
}
if(ddis[s.to]>d2 && dis[s.to]<d2){
ddis[s.to]=d2;
que.push(P(ddis[s.to],s.to));
}
}
}
printf("%d\n",ddis[N-]);
} int main()
{
int from,to,cost;
node e;
while(~scanf("%d%d",&N,&R)){
while(R--){
scanf("%d%d%d",&from,&to,&cost);
from--;to--;
e.to=to;e.cost=cost;
g[from].push_back(e);
e.to=from;
g[to].push_back(e);
}
solve();
}
}
 

Poj Roadblocks(次短路)的更多相关文章

  1. Bzoj 1726: [Usaco2006 Nov]Roadblocks第二短路 dijkstra,堆,A*,次短路

    1726: [Usaco2006 Nov]Roadblocks第二短路 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 969  Solved: 468[S ...

  2. BZOJ1726: [Usaco2006 Nov]Roadblocks第二短路

    1726: [Usaco2006 Nov]Roadblocks第二短路 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 768  Solved: 369[S ...

  3. BZOJ 1726: [Usaco2006 Nov]Roadblocks第二短路( 最短路 )

    从起点和终点各跑一次最短路 , 然后枚举每一条边 , 更新answer ---------------------------------------------------------------- ...

  4. BZOJ 1726: [Usaco2006 Nov]Roadblocks第二短路

    1726: [Usaco2006 Nov]Roadblocks第二短路 Description 贝茜把家搬到了一个小农场,但她常常回到FJ的农场去拜访她的朋友.贝茜很喜欢路边的风景,不想那么快地结束她 ...

  5. 1726: [Usaco2006 Nov]Roadblocks第二短路

    1726: [Usaco2006 Nov]Roadblocks第二短路 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 835  Solved: 398[S ...

  6. 最短路【bzoj1726】: [Usaco2006 Nov]Roadblocks第二短路

    1726: [Usaco2006 Nov]Roadblocks第二短路 Description 贝茜把家搬到了一个小农场,但她常常回到FJ的农场去拜访她的朋友.贝茜很喜欢路边的风景,不想那么快地结束她 ...

  7. Heavy Transportation POJ 1797 最短路变形

    Heavy Transportation POJ 1797 最短路变形 题意 原题链接 题意大体就是说在一个地图上,有n个城市,编号从1 2 3 ... n,m条路,每条路都有相应的承重能力,然后让你 ...

  8. poj - 3225 Roadblocks(次短路)

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

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

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

随机推荐

  1. hidden change事件

    原文:hidden change事件 对于隐藏域hidden无法触发onchange的解决方法:在更改此隐藏域的时候,调用下它的onchange方法,使用jquery的话, 就直接加上 $(" ...

  2. wamp无法登录phpmyadmin问题

    文章来源:PHP座谈会 地址:http://bbs.phpthinking.com/forum.php? mod=viewthread&tid=163 第一步.用navicat确认一下,自己的 ...

  3. sails中文文档地址

    http://sailsdoc.swift.ren/ Sails.js是一个Web框架,可以于轻松构建自定义,企业级Node.js Apps.它在设计上类似于像Ruby on Rails的MVC架构的 ...

  4. js缓冲运动

    缓冲运动 现象:逐渐变慢,最后停止 原理:距离越远,速度越大 速度的计算方式: 1,速度由距离决定 2,速度=(目标值-当前值)/缩放系数 说明:速度为正负数时,也决定了物体移动的方向 示例:div缓 ...

  5. android studio 怎样正确导入jar

    近期又開始做android,使用android studio中遇到导入jar没有反应的问题,查了下资料实践攻克了,现特地写一下博客.希望对刚刚的使用的android studio的朋友有帮助. 1.先 ...

  6. Spring Assert主张 (参议院检测工具的方法-主张)

    Web 收到申请表格提交的数据后都需要对其进行合法性检查,假设表单数据是不合法的,该请求将被拒绝.分类似的,当我们写的类方法,该方法还经常需要组合成参 法国检查.假设参议院不符合要求,方法通过抛出异常 ...

  7. Windows 8实例教程系列 - 布局控制

    原文:Windows 8实例教程系列 - 布局控制 与传统应用类似,Windows store应用允许开发人员通过布局控件管理应用UI. 本篇将讨论Windows8布局设计控制. Windows 8布 ...

  8. Windows Auzre 微软的云计算产品的后台操作界面

    Windows Auzre 微软的云计算产品的后台操作界面,试用期,相比于阿里云后台操作不是人. watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvTmFvbG ...

  9. 古老server源代码迁移到新server

    因为老vsts资源server不久,准备存档,现在在旧的需要server该代码仍然在使用的所有迁移到新的vstsserver在. 因此,我们需要迁移所有需要也许是习惯了新的代码vsts在之上.代码的迁 ...

  10. Iterator、Iterable接口的使用及详解

    Java集合类库将集合的接口与实现分离.同样的接口,可以有不同的实现. Java集合类的基本接口是Collection接口.而Collection接口必须实现Iterator接口. 以下图表示集合框架 ...