Silver Cow Party(2013.09.15)(最短路)

本文介绍了一种使用SPFA算法解决特定场景下最短路径问题的方法,具体为求解农场间牛参加聚会往返所需的最长行走时间。通过双向建图优化了路径搜索过程。

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

G. Silver Cow Party

2000ms
2000ms
65536KB
64-bit integer IO format:  %lld      Java class name:  Main
Font Size:   

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 irequires 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:  AiBi, 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
 
 
 
 
这道题其实是一道最短路的变形。在建图的时候正反各建立一次,然后用spfa求得相应的距离。
这里要注意一下,以后建图的时候尽可能用邻接表来建立。(可以用数组,可以用vector、list)
 
 
 
 
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#include<vector>
#define INF 10000000
using namespace std;
int n,m,x,dist_v1[1010]= {0},dist_v2[1010]= {0};
struct node
{
    int x,d;
};
vector<node> v1[1010];
vector<node> v2[1010];
void spfa_v1(int x)
{
    int i,j,visit[1010]= {0},t;
    queue<int> q;
    for (i=1; i<=n; i++)
        dist_v1[i]=INF;
    dist_v1[x]=0;
    visit[x]=1;
    q.push(x);
    int s,l;
    while(!q.empty())
    {
        t=q.front();
        q.pop();
        visit[t]=0;
        for (i=0; i<v1[t].size(); i++)
        {
            s=v1[t][i].x;
            l=v1[t][i].d;
            if (dist_v1[s]>dist_v1[t]+l)
            {
                dist_v1[s]=dist_v1[t]+l;
                if (!visit[s])
                {
                    visit[s]=1;
                    q.push(s);
                }
            }
        }
    }
}
void spfa_v2(int x)
{
    int i,j,visit[1010]= {0},t;
    queue<int> q;
    for (i=1; i<=n; i++)
        dist_v2[i]=INF;
    dist_v2[x]=0;
    visit[x]=1;
    q.push(x);
    int s,l;
    while(!q.empty())
    {
        t=q.front();
        q.pop();
        visit[t]=0;
        for (i=0; i<v2[t].size(); i++)
        {
            s=v2[t][i].x;
            l=v2[t][i].d;
            if (dist_v2[s]>dist_v2[t]+l)
            {
                dist_v2[s]=dist_v2[t]+l;
                if (!visit[s])
                {
                    visit[s]=1;
                    q.push(s);
                }
            }
        }
    }
}
int main ()
{
    int i,j;
    int a,b,t;
    scanf("%d%d%d",&n,&m,&x);
    node e;
    while(m--)
    {
        scanf("%d%d%d",&a,&b,&t);
        e.x=b;
        e.d=t;
        v1[a].push_back(e);
        e.x=a;
        v2[b].push_back(e);
    }
    spfa_v1(x);
    spfa_v2(x);
    int max=0,s1,s2;
    for (i=1; i<=n; i++)
    {
        if (max<dist_v1[i]+dist_v2[i])
            max=dist_v1[i]+dist_v2[i];
    }
    cout<<max<<endl;
    return 0;
}


 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值