http://poj.org/problem?id=3255

同匈牙利游戏。

但是我发现了一个致命bug。

就是在匈牙利那篇,应该dis2单独if,而不是else if,因为dis2和dis1相对独立。有可能在前边两个if改了后还有更优的次短路。

所以,,wikioi那题太水,让我水过了。。

#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << #x << " = " << x << endl
#define printarr(a, n, m) rep(aaa, n) { rep(bbb, m) cout << a[aaa][bbb]; cout << endl; }
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }
inline const int max(const int &a, const int &b) { return a>b?a:b; }
inline const int min(const int &a, const int &b) { return a<b?a:b; } const int N=5050;
const long long oo=~0ull>>2;
int m, n, vis[N], q[N], front, tail, ihead[N], cnt;
long long d[N], d2[N];
struct ED { int to, next; long long w; }e[200010];
inline void add(const int &u, const int &v, const int &w) {
e[++cnt].next=ihead[u]; ihead[u]=cnt; e[cnt].to=v; e[cnt].w=w;
}
long long spfa(const int &s, const int &t) {
for1(i, 0, t) d[i]=d2[i]=oo;
d[s]=front=tail=0; vis[s]=1; q[tail++]=s;
int u, v, w;
while(front!=tail) {
u=q[front++]; if(front==N) front=0; vis[u]=0;
for(int i=ihead[u]; i; i=e[i].next) {
v=e[i].to; w=e[i].w;
if(d[v]>d[u]+w) {
d2[v]=d[v]; d[v]=d[u]+w;
if(!vis[v]) { vis[v]=1; q[tail++]=v; if(tail==N) tail=0; }
}
else if(d2[v]>d[u]+w && d[v]<d[u]+w) {
d2[v]=d[u]+w;
if(!vis[v]) { vis[v]=1; q[tail++]=v; if(tail==N) tail=0; }
}
if(d2[v]>d2[u]+w) {
d2[v]=d2[u]+w;
if(!vis[v]) { vis[v]=1; q[tail++]=v; if(tail==N) tail=0; }
}
}
}
if(d2[t]!=oo) return d2[t];
return -1;
} int main() {
read(n); read(m);
int x, y, z;
rep(i, m) {
read(x); read(y); read(z);
add(x, y, z); add(y, x, z);
}
printf("%lld", spfa(1, n));
return 0;
}

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: A, B, 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

【POJ】3255 Roadblocks(次短路+spfa)的更多相关文章

  1. POJ 3255 Roadblocks (次短路 SPFA )

    题目链接 Description Bessie has moved to a small farm and sometimes enjoys returning to visit one of her ...

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

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

  3. poj 3255 Roadblocks 次短路(两次dijksta)

    Roadblocks Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) Total S ...

  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. 深度学习入门教程UFLDL学习实验笔记一:稀疏自编码器

    UFLDL即(unsupervised feature learning & deep learning).这是斯坦福网站上的一篇经典教程.顾名思义,你将在这篇这篇文章中学习到无监督特征学习和 ...

  2. Linux 实现rsyslog日志里面的IP地址记录 未测试

    之前我是在bashrc中添加了一句,让系统操作日志时向rsyslog发送一份内容,现在只要在发送的时候,自己再获取下当前的远程登录IP加进去就可以,像这样 /etc/bashrc sshClientI ...

  3. /var/lock/subsys作用

    转自: http://sunxiaqw.blog.163.com/blog/static/9906543820111184422807/ 关于/var/lock/subsys目录 总的来说,系统关闭的 ...

  4. PHP 逻辑思维题

    约瑟夫环 一群猴子排成一圈,按1,2,...,n依次编号.然后从第1只开始数,数到第m只,把它踢出圈,从它后面再开始数,再数到第m只,在把它踢出去...,如此不停的进行下去,直到最后只剩下一只猴子为止 ...

  5. Shell编程基础

    写之前我们先来搞清楚为什么要学shell,学习要有目的性shell简单.灵活.高效,特别适合处理一些系统管理方面的小问题shell可以实现自动化管理,让系统管理员的工作变得容易.简单.高效shell脚 ...

  6. selec2 clone不起作用。

    <table class="table table-bordered"> <thead> <tr> <th width ="16 ...

  7. Java面向对象的多态

    Java中多态的概念是面向对象中除封装和继承外非常重要的知识点,也是Java面向对象三大特性最后一个特性 多态其实就是指对象存在的多种形态,多态分为引用多态和方法多态 引用多态的含义就是:父类的引用可 ...

  8. JS Replace 全部替换字符 用法

    转载自:http://www.cnblogs.com/skykang/archive/2011/08/04/2127158.html <script language="javascr ...

  9. Step deep into GLSL

    1 Lighting computation is handled in eye space(需要根据眼睛的位置来计算镜面发射值有多少进入眼睛), hence, when using GLSL (GP ...

  10. 火车站(codevs 2287)

    题目描述 Description 火车从始发站(称为第1站)开出,在始发站上车的人数为a,然后到达第2站,在第2站有人上.下车,但上.下车的人数相同,因此在第2站开出时(即在到达第3站之前)车上的人数 ...