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. Windows Phone 8 - Runtime Location API - 1

    原文:Windows Phone 8 - Runtime Location API - 1 在Windows Phone里要做Background Service的方式,除了Background Ag ...

  2. 02、Unicode 汉子转码小工具

    原文:02.Unicode 汉子转码小工具 在做 Windows app 的时候,与服务器端交互使用的是 json 格式的数据,里面的汉字内容被 编码成 unicode 格式,在调试的时候不太方便,就 ...

  3. VS2012 编译程序时报无法载入PDB文件错误解决方式

    VS2012 编译程序时报无法载入PDB文件错误解决方式 "ConsoleApplication1.exe"(Win32): 已载入"C:\Users\hp\Docume ...

  4. 【转】static_cast和reinterpret_cast

    static_cast和reinterpret_cast揭秘 收藏 本文讨论static_cast<> 和 reinterpret_cast<>. reinterpret_ca ...

  5. 什么是PV,UV。

    PV浏览(Page View).该网页访问量,每次页面打开PV统计+1,也刷新. IP接入号码指独立IP接入号码,计算基于独立IP在计算的时间段来计算访问我们的网站1二级IP接入号码. 是否这个计算在 ...

  6. 【原版的】PHP技术成长规划过程中猿人

    PHP程序猿的技术成长规划 作者:黑夜路人(2014/10/15) 依照了解的非常多PHP/LNMP程序猿的发展轨迹.结合个人经验体会,抽象出非常多程序猿对未来的迷漫,特别对技术学习的盲目和慌乱.简单 ...

  7. AutoMapper在ABP框架

    AutoMapper在ABP框架中的使用说明 为了说明AutoMapper如何使用,我专门开设了一个专题来讲,如果您还没有查看该专题,请点击这里.既然系统地学习了AutoMapper,那么接下来就是该 ...

  8. Java爬虫,信息抓取的实现(转)

    转载请注明出处:http://blog.csdn.net/lmj623565791/article/details/23272657 今天公司有个需求,需要做一些指定网站查询后的数据的抓取,于是花了点 ...

  9. httpclient 文件上传

    /**      * 上传文件      */     public static Boolean  uploadFile(String fileName, String url) {         ...

  10. Android之Handler的postDelayed()使用方法

    这是一种创建多线程信息功能 用法: 1,首先创建一个Handler物 Handler handler=new Handler(); 2.然后创建一个Runnable物 Runnable runnabl ...