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)
 
dijkatra算法可以求最短路和次短路
 #include"iostream"
#include"cstdio"
#include"cstring"
#include"algorithm"
#include"cstdlib"
#include"queue"
#include"vector"
using namespace std;
const int inf=0x7fffffff;
const int ms=;
struct edge
{
//int from;
int to;
int cost;
};
typedef pair<int,int> P;
int main()
{
int i,j,N,R,ans,a,b,w;
scanf("%d%d",&N,&R);
vector<edge> vec[N+];
for(i=;i<=R;i++)
{
scanf("%d%d%d",&a,&b,&w);
edge e;
e.to=b;
e.cost=w;
//vec[a].push_back(edge{b,w}); 有警告
vec[a].push_back(e);
e.to=a;
vec[b].push_back(e);
}
int dist[N+],dist2[N+];
priority_queue<P,vector<P>,greater<P> > que;
fill(dist,dist+N+,inf);
fill(dist2,dist2+N+,inf);
dist[]=;
que.push(P(,));
while(!que.empty())
{
P p=que.top();
que.pop();
int v=p.second;
int d=p.first;
if(dist2[v]<d)
continue;
for(i=;i<vec[v].size();i++)
{
edge &e=vec[v][i];
int d2=d+e.cost;
if(dist[e.to]>d2)
{
swap(dist[e.to],d2);
que.push(P(dist[e.to],e.to));
}
if(dist2[e.to]>d2&&dist[e.to]<d2)
{
dist2[e.to]=d2;
que.push(P(dist2[e.to],e.to));
}
}
}
//printf("%d\n",dist2[N]);
cout<<dist2[N]<<endl;
return ;
}

Roadblocks http://poj.org/problem?id=3255的更多相关文章

  1. poj 1651 http://poj.org/problem?id=1651

      http://poj.org/problem?id=1651Multiplication Puzzle   Time Limit: 1000MS   Memory Limit: 65536K To ...

  2. poj-3056 http://poj.org/problem?id=3056

    http://poj.org/problem?id=3056 The Bavarian Beer Party Time Limit: 6000MS   Memory Limit: 65536K Tot ...

  3. poj 1679 http://poj.org/problem?id=1679

    http://poj.org/problem?id=1679 The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submis ...

  4. POJ3278http://poj.org/problem?id=3278

    http://poj.org/problem?id=3278 题目大意: m,n两个数m可+1, -1, *2变成n,需要经过几步 #include<stdio.h> #include&l ...

  5. 最小生成树 10.1.5.253 1505 poj 1258 http://poj.org/problem?id=1258

    #include <iostream>// poj 1258 10.1.5.253 1505 using namespace std; #define N 105 // 顶点的最大个数 ( ...

  6. http://poj.org/problem?id=3278(bfs)

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 76935   Accepted: 24323 ...

  7. poj 1915 http://poj.org/problem?id=1915

    /**< */#include <stdio.h> #include <string.h> #include <stdlib.h> #include < ...

  8. http://poj.org/problem?id=2253

    floyd的应用求每条路径两点之间最大距离的最小值 #include <iostream> #include <cstdio> #include <algorithm&g ...

  9. 线段树 (区间更新,区间查询) poj http://poj.org/problem?id=3468

    题目链接 #include<iostream> #include<cstdio> #include<cmath> #include<cstdlib> # ...

随机推荐

  1. bzoj 3505 [Cqoi2014]数三角形(组合计数)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3505 [题意] 在n个格子中任选3点构成三角形的方案数. [思路] 任选3点-3点共线 ...

  2. 我是怎么发现并解决项目页面渲染效率问题的(IE调试工具探查器的使用)

    #我是怎么发现并解决项目页面渲染效率问题的(IE调试工具探查器的使用) ##背景 之前的项目中,有很多的登记页面,一般都有100-200甚至更加多的字段,而且还涉及到字典.日期及其他效果的显示,载入时 ...

  3. Hadoop中FileSystem的append方法

    今天在使用Hadoop 1.1.2版本进行FileSystem的append操作时报以下异常: org.apache.hadoop.ipc.RemoteException: java.io.IOExc ...

  4. Windows10输入法的切换

    Alt+Shift            中⇒あ,あ⇒中 Shift                  中⇒英,英⇒中 Alt+Caps Lock    あ⇒カ,A⇒あ⇒カ Ctrl+Caps Loc ...

  5. Junit3.8

    使用Junit的最佳实践:新建一个名为test的source folder,用于存放测试类源代码目标类与测试类应该位于同一个包下面,这样测试类中就不必导入源代码所在的包,因为他们位于同一个包下面 测试 ...

  6. Git 一些日常使用积累

    本来不想写这样的东西的,因为随处谷歌百度都有一大堆!但是,我却总是在百度谷歌,我在想,为什么我不自己写一篇存进来,顺便加深印象呢?既然这样,这篇随笔,就真的变成随笔好了,随时修改,随时添加. Git ...

  7. [转]Oracle 操作字符串的函数

    转至:http://yedward.net/?id=62 (1)oracle中实现截取字符串:substr substr(string, start_position, [length]) 其中,st ...

  8. presentedViewController 和 presentingViewController 以及 dismissViewControllerAnimated 的使用

    在日常的开发中,多控制器之间的跳转除了使用push的方式,还可以使用 present的方式,present控制器时,就避免不了使用 presentedViewController.presenting ...

  9. iOS开发-为程序添加应用设置

    一.设置捆绑包 设置捆绑包是应用自带的一组文件,用于告诉设置该应用期望得到用户的哪些偏好设置. 新建设置捆绑包:Command+N,在iOS部分中的Resource,选择Settings Bundle ...

  10. Educational Codeforces Round 1 A. Tricky Sum 暴力

    A. Tricky Sum Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/598/problem ...