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

Posted wenzhixin

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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
",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 }

 


以上是关于POJ 3268 Silver Cow Party(Dijkstra算法求解来回最短路问题)的主要内容,如果未能解决你的问题,请参考以下文章

POJ 3268 Silver Cow Party (Dijkstra)

POJ3268 Silver Cow Party —— 最短路

POJ 3268 Silver Cow Party

POJ 3268 Silver Cow Party

poj 3268 Silver Cow Party

POJ-3268 Silver Cow Party( 最短路 )