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

4329 ワード

http://poj.org/problem?id=3255
ハンガリーとゲームをします.
しかし、致命的なバグを発見しました.
ハンガリーでは、dis 2はシングルifではなく、else ifではなく、dis 2とdis 1は相対的に独立しています.前の2つのifを変更した後、より良いショートがあります.
だから、ワイキオは水が多すぎて、水が過ぎました.
#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 d to a ssmaall farm and sometimes enjoys returning to visit one of her best friinds.She dost wantto to get to her old home too quickly、because she she she e likes the sceneralalininingtheway.She thethethethethethethethethethethethethethethethethethethethethethethekkkhatststsshshshshshshshshshshshshshshshshshshshshsheeeeeeeeeshshshshshshshshshshshshshshshshshshshshshshshshshshsheeeeeeeeth.
The countryside consists of R(1≦R≦100,000)bidirection roads、each linking two of the N(1≦N≦5000)intersections、conventlynumberd 1.N.Bessie starts at intersection 1、and her friend(the stination.)
The second-sharest path may share road s with any of the shotest paths,and it may backtrack i.e.use the same road or intersetion mone.The second-shotest path is the shot partest path path th th th th partest(i.e.if two or more shot paths exist、the second-shotest path is the one whose length is longer than butのlonger thany 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 intersects A and B and has length D(1≦D≦5000)
Output
Line 1:The length of the second shotest 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
ベント
Two routes:1->>2->4(length 100+200=300)and 1->2->3->4(length 100+250+100=450)
ソurce
USACO 2006 November Gold