日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

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

發布時間:2025/3/17 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【POJ】3255 Roadblocks(次短路+spfa) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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

USACO 2006 November Gold

總結

以上是生活随笔為你收集整理的【POJ】3255 Roadblocks(次短路+spfa)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。