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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【POJ - 3268 】Silver Cow Party(Dijkstra最短路+思维)

發布時間:2023/12/10 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【POJ - 3268 】Silver Cow Party(Dijkstra最短路+思维) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題干:

One cow from each of?N?farms (1 ≤?N?≤ 1000) conveniently numbered 1..N?is going to attend the big cow party to be held at farm #X?(1 ≤?X?≤?N). A total of?M?(1 ≤?M?≤ 100,000) unidirectional (one-way roads connects pairs of farms; road?i?requires?Ti?(1 ≤?Ti?≤ 100) units of time to traverse.

Each cow must walk to the party and, when the party is over, return to her farm. Each cow is lazy and thus picks an optimal route with the shortest time. A cow's return route might be different from her original route to the party since roads are one-way.

Of all the cows, what is the longest amount of time a cow must spend walking to the party and back?

Input

Line 1: Three space-separated integers, respectively:?N,?M, and?X?
Lines 2..?M+1: Line?i+1 describes road?i?with three space-separated integers:Ai,?Bi, and?Ti. The described road runs from farm?Ai?to farm?Bi, requiring?Titime units to traverse.

Output

Line 1: One integer: the maximum of time any one cow must walk.

Sample Input

4 8 2 1 2 4 1 3 2 1 4 7 2 1 1 2 3 5 3 1 2 3 4 4 4 2 3

Sample Output

10

Hint

Cow 4 proceeds directly to the party (3 units) and returns via farms 1 and 3 (7 units), for a total of 10 time units.

題目大意:

從出發點到達目的地X,再從X返回到出發點的最短路徑中的最大值(因為出發點沒有固定,也就是可以:1->X->1, 2->X->2等)。

解題報告:

? ?跑兩遍最短路,求一個x到各個點的,再求轉置矩陣然后跑一邊x到各個點的,也就是原矩陣的各個點到x點的。加起來維護最大值即可。

一個題解:所以我們要枚舉所有的可能性,找出其中的最大值。巧妙地運用dijkstra算法,雙向求出兩次X->m的最短路徑長然后相加即得到了m->X->m的最短路徑。

AC代碼:

#include<cstdio> #include<cstring> #include<algorithm> #include<queue> #include<iostream> using namespace std; const int MAX = 100000 + 5; const int INF = 0x3f3f3f3f ; struct Node {int to;int w;int ne; } e[MAX]; int a[100000 + 5],b[100000 + 5],w[100000 + 5]; struct point {int pos;int c;point(){}//沒有此構造函數不能寫 node t 這樣point(int pos,int c):pos(pos),c(c){}//可以寫node(pos,cost)這樣bool operator <(const point & b) const {return c>b.c;} };int head[MAX]; int vis[MAX]; int cnt = 0; int maze[1005][1005]; int n,m; int dis1[MAX],dis2[MAX];//表示從出發點開始到該點的最短距離。 void add(int u,int v,int w) {e[cnt].to = v;e[cnt].w = w;e[cnt].ne = head[u];head[u] = cnt;cnt++; } void Dijkstra1(int u) {priority_queue<point> pq; dis1[u] = 0;point cur = point(u,0);pq.push(cur);while(!pq.empty()) {point now = pq.top();pq.pop();vis[now.pos] = 1;for(int i = head[now.pos]; i!=-1; i=e[i].ne) {if( dis1[e[i].to] > dis1[now.pos] + e[i].w ) {dis1[e[i].to] = dis1[now.pos] + e[i].w;pq.push(point(e[i].to,dis1[e[i].to] ) ); }} } } void Dijkstra2(int u) {priority_queue<point> pq; dis2[u] = 0;point cur = point(u,0);pq.push(cur);while(!pq.empty()) {point now = pq.top();pq.pop();vis[now.pos] = 1; for(int i = head[now.pos]; i!=-1; i=e[i].ne) {if( dis2[e[i].to] > dis2[now.pos] + e[i].w ) {dis2[e[i].to] = dis2[now.pos] + e[i].w;pq.push(point(e[i].to,dis2[e[i].to] ) ); }} } } void init1() {cnt = 0;memset(head,-1,sizeof(head) );memset(dis1,INF,sizeof(dis1) ) ;memset(maze,INF,sizeof(maze) );memset(vis,0,sizeof(vis) ) ; } void init2() {cnt = 0;memset(head,-1,sizeof(head) );memset(dis2,INF,sizeof(dis2) ) ;memset(maze,INF,sizeof(maze) );memset(vis,0,sizeof(vis) ) ; } int main() {int x;while(~scanf("%d %d %d",&n,&m,&x) ) {init1();for(int i = 1; i<=m; i++) {scanf("%d%d%d",&a[i],&b[i],&w[i]);if(w[i]<maze[a[i] ][b[i] ]) {maze[a[i]][b[i]] = w[i];add(a[i],b[i],w[i]);}}Dijkstra1(x);init2();for(int i = 1; i<=m; i++) {if(w[i]<maze[b[i] ][a[i] ]) {maze[b[i]][a[i]] = w[i];add(b[i],a[i],w[i]);}}Dijkstra2(x);int maxx = 0;for(int i = 1; i<=n; i++) {maxx = max(maxx,dis1[i] + dis2[i]);} // printf("::::::::\n");printf("%d\n",maxx);}return 0 ;}

總結:

? ?其實我這里寫麻煩了 ,如果用鄰接矩陣保存圖的話,直接求轉置矩陣然后再最短路一發就行了。如果用鄰接表這樣,就得先保存輸入數據然后跑一邊后再初始化然后再讀入一遍再跑一遍。

?

總結

以上是生活随笔為你收集整理的【POJ - 3268 】Silver Cow Party(Dijkstra最短路+思维)的全部內容,希望文章能夠幫你解決所遇到的問題。

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