POJ 3268 Silver Cow Party(Dijkstra算法求解来回最短路问题)

题目链接:

https://vjudge.net/problem/POJ-3268

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 ≤ XN). 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
Ti time 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.

 1 /*
 2     关键是反向存储,求最短路的思维转换
 3 */
 4 #include<stdio.h>
 5 #include<string.h>
 6 #include<algorithm>
 7 using namespace std;
 8 void Dijkstra(int s,int e[][1010]);
 9 #define inf 99999999
10 int dis[1010],book[1010],e[1010][1010],f[1010][1010],d[1010],n,m;
11 int main()
12 {
13     int i,j,k,x,a,b,c,maxn,temp;
14     while(scanf("%d%d%d",&n,&m,&x)!=EOF)
15     {
16         memset(d,0,sizeof(d));
17         for(i=1;i<=n;i++)
18             for(j=1;j<=n;j++)
19                 if(i==j)
20                 {
21                     e[i][j]=0;
22                     f[i][j]=0;
23                 }
24                 else
25                 {
26                     e[i][j]=inf;
27                     f[i][j]=inf;
28                 }
29
30         for(i=1;i<=m;i++)
31         {
32             scanf("%d%d%d",&a,&b,&c);
33             if(c<e[a][b])
34             {
35                 e[a][b]=c;
36                 f[b][a]=c;
37             }
38         }
39         Dijkstra(x,e);//回
40         Dijkstra(x,f);//去
41         maxn=-1;
42         for(i=1;i<=n;i++)
43         {
44             if(d[i]>maxn)
45                 maxn=d[i];
46         }
47         printf("%d\n",maxn);
48     }
49     return 0;
50 }
51 void Dijkstra(int s,int e[][1010])
52 {
53     int i,j,k,min,u;
54     for(i=1;i<=n;i++)
55         dis[i]=e[s][i];
56     memset(book,0,sizeof(book));
57     book[s]=1;
58     for(k=1;k<n;k++)
59     {
60         min=inf;
61         for(i=1;i<=n;i++)
62             if(book[i]==0&&dis[i]<min)
63             {
64                 min=dis[i];
65                 u=i;
66             }
67         book[u]=1;
68         for(i=1;i<=n;i++)
69             if(book[i]==0&&dis[i]>dis[u]+e[u][i])
70                 dis[i]=dis[u]+e[u][i];
71     }
72     for(i=1;i<=n;i++)
73         d[i]+=dis[i];
74 }

原文地址:https://www.cnblogs.com/wenzhixin/p/9429531.html

时间: 2024-10-13 21:05:53

POJ 3268 Silver Cow Party(Dijkstra算法求解来回最短路问题)的相关文章

POJ 3268 Silver Cow Party dijkstra单源最短路

裸dijkstra 思路:以x为源点,求到其他点的最短路,之后把邻接矩阵转置,再求一次x源 点的最短路,这样就一次是来的,一次是走的,相加迭代最大值即可 代码: /* poj 3268 8108K 47MS */ #include<cstdio> #include<iostream> #define MAXN 1005 #define MAX_INT 2147483647 using namespace std; int gra_in[MAXN][MAXN],gra_out[MAX

POJ 3268 Silver Cow Party (Dijkstra)

题目链接:POJ 3268 Description 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

poj 3268 Silver Cow Party(dijkstra||SPFA)(中等)

Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 14900   Accepted: 6746 Description 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 

POJ 3268 Silver Cow Party 最短路—dijkstra算法的优化。

POJ 3268 Silver Cow Party Description 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

poj 3268 Silver Cow Party , spfa , dijkstra

点击打开链接 两次求最短路(第二次把边反向求) 1.spfa //poj 3268 Silver Cow Party //SPFA #include <cstdio> #include <cstring> #include <queue> using namespace std; const int M = 100000 + 100; const int N = 1000 + 100; const int inf = 1<<25; struct Graph

POJ 3268 Silver Cow Party(SPFA)

Description 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 re

图论 ---- spfa + 链式向前星 ---- poj 3268 : Silver Cow Party

Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 12674   Accepted: 5651 Description 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 

poj 3268 Silver Cow Party(最短路)

poj 3268 Silver Cow Party Description 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

POJ 3268 Silver Cow Party(最短路dijkstra)

Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 15451   Accepted: 6997 Description 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