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

本文详细解析了如何使用反向存储和Dijkstra算法解决POJ-3268题目,即计算N个农场中每头牛参加聚会并返回的最长总时间。通过实例输入输出,展示了算法的具体实现过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接:

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值