【POJ】3255 Roadblocks(次短路+spfa)
http://poj.org/problem?id=3255
同匈牙利游戲。
但是我發(fā)現了一個致命bug。
就是在匈牙利那篇,應該dis2單獨if,而不是else if,因為dis2和dis1相對獨立。有可能在前邊兩個if改了后還有更優(yōu)的次短路。
所以,,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 RLines 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 NSample Input
4 4 1 2 100 2 4 200 2 3 250 3 4 100Sample Output
450Hint
Two routes: 1 -> 2 -> 4 (length 100+200=300) and 1 -> 2 -> 3 -> 4 (length 100+250+100=450)Source
USACO 2006 November Gold總結
以上是生活随笔為你收集整理的【POJ】3255 Roadblocks(次短路+spfa)的全部內容,希望文章能夠幫你解決所遇到的問題。
                            
                        - 上一篇: .Net开源源码查询
 - 下一篇: Spring Aop 切点表达式