码迷,mamicode.com
首页 > 数据库 > 详细

【POJ】3255 Roadblocks(次短路+spfa)

时间:2014-08-27 10:48:17      阅读:300      评论:0      收藏:0      [点我收藏+]

标签:des   blog   http   os   io   for   ar   art   div   

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)

标签:des   blog   http   os   io   for   ar   art   div   

原文地址:http://www.cnblogs.com/iwtwiioi/p/3938831.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!