Silver Cow Party

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:  NM, 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.

题意:有很多来自不同农场的牛要去参加聚会,聚会举行在农场X,每一头牛都要去参加聚会并到自己的农场,还有就是每一头牛都很懒,所以它们走的都会是耗时最短的路,让我们求耗时最长的牛总共花了多长时间。

第一行3个数字n、m、x,表示总共n头牛、m条路线、聚会在x农场举行,接下来m行分别是从A农场到B农场耗时t。注意这是单向的。

思路:2个dijkstra分别计算从i农场到x农场、从x农场回到i农场的最小耗时,然后选择相加耗时最大的就好

代码:

#include<stdio.h>
#include<string.h>
#define inf 0x3f3f3f
#include<algorithm>
using namespace std;
int n,m,x,a,b,t,ans=0,e[1200][1200],dis1[1200],dis2[1200],vis1[1200],vis2[1200];
int main()
{
    scanf("%d %d %d",&n,&m,&x);
    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=n; j++)
            e[i][j]=(i==j)?0:inf;
    }
    for(int i=0; i<m; i++)
    {
        scanf("%d %d %d",&a,&b,&t);
        e[a][b]=min(e[a][b],t);
    }
    for(int i=1;i<=n;i++)
    {
        dis1[i]=e[i][x];//dis1记录的是从i农场到x农场的最小耗时
        dis2[i]=e[x][i];//dis2记录的是从x农场回到i农场的最小耗时
    }
    for(int i=1;i<=n;i++)
    {
        int k1,k2,mi1=inf,mi2=inf;
        for(int j=1;j<=n;j++)
        {
            if(vis1[j]==0&&dis1[j]<mi1)
            {
                mi1=dis1[j];
                k1=j;
            }
            if(vis2[j]==0&&dis2[j]<mi2)
            {
                mi2=dis2[j];
                k2=j;
            }
        }
        vis1[k1]=1;
        vis2[k2]=1;
        for(int j=1;j<=n;j++)
        {
            dis1[j]=min(dis1[j],dis1[k1]+e[j][k1]);//最开始写成dis1[k1]+e[k1][j],WA了一发,dis1[k1]+e[k1][j]的意思是k1→x+k1→j,而我们要的是j→k1+k1→x
            dis2[j]=min(dis2[j],dis2[k2]+e[k2][j]);
        }
    }
    for(int i=1; i<=n; i++)
        ans=max(ans,dis1[i]+dis2[i]);//我们要的是来回共耗时最大的一个~~~
    printf("%d\n",ans);
    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值