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. JAVA基础编程50题(10-12题)具体解释

    一.描写叙述 1.一球从m米高度自由落下.每次落地后反跳回原高度的一半:再落下,求它在 第n次落地时.共经过多少米?第10次反弹多高? 2.有1.2.3.4个数字.能组成多少个互不同样且无反复数字的三 ...

  2. 经excel要将数据库(ORACLE)要插入数据

    大家都知道PL/SQL可以excel数据复制.我们也可以通过相同excel将数据插入到数据库. 下面我们就来简单的样品,并与主题演示 首先,我们创建了一个表test CREATE TABLE test ...

  3. Children’s Queue

    Children's Queue Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  4. lua、groovy嵌入到java中的性能对比(转)

    lua和groovy都是可以嵌入到java中的脚本语言.lua以高性能著称,与C/C++在游戏开放中有较多使用,groovy是一个基于Java虚拟机(JVM)的敏捷动态语言,在jvm下有着不错的性能. ...

  5. webclient下载文件 带进度条

    private void button1_Click(object sender, EventArgs e) { doDownload(textBox1.Text.Trim()); } private ...

  6. 什么是Cyclomatic Complexity(圈复杂度)?

    Campwood Software SourceMonitor Version 3.5 The freeware program SourceMonitor lets you see inside y ...

  7. 有一个NSStirng类型,retain时尚宣言name财产setter内部方法的每一行代码的作用?

    - (void)setName:(NSString *)name {        推断原有对象和新对象是否是同一个对象,假设是同一个,就没有必要再又一次赋值,否则会先release 再retain, ...

  8. 2014阿里巴巴web前实习生项目分析(1)

    以下简化CSS代码: div.container{ width:500px; background-image:url(/img/sprite.png); background-repeat:no-r ...

  9. 探索Windows Azure 监控和自动伸缩系列1 - 连接中国区Azure

    最近准备基于Microsoft Azure Management Libraries 实现虚拟机的监控.主要的需求就是获取虚拟机内置的性能计数器数据,基于性能计数器实现后续的监控和自动伸缩. 作为这一 ...

  10. the apple tree

    the apple tree A long time ago, there was a huge apple tree. A little boy loved to come and lay arou ...