POJ-3268 Silver Cow Party(最短路spfa算法)

版权声明:Copyright : 李三金,原创文章转载标明出处即可。 https://blog.csdn.net/santa9527/article/details/54425028

POJ-3268 Silver Cow Party

Silver Cow Party
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 19989 Accepted: 9128

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 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.

Source
USACO 2007 February Silver

N只牛(初始都待在自己序号所在的农场),M条路 X是举办party的地点,除了在该点的牛,其余牛都要从他们的农场到该点参加party,然后回到自己的农场,每只牛都走最短路,你要找出走了最多路的牛。

路不是双向,所以要找两次最短路,i个点到x的最短路,然后把反过来再找一次。然后找出两次和最大的牛就可以了。

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
#include<queue>
#define INF 0x3f3f3f3f
using namespace std;
int d1[1024],d2[1024],w[1024][1024];//d1第一次的最短路,d2第二次的最短路,w两点距离
bool v[1024];
int n,m,sx;
void spfa(int *d)
{
    int i,j;
    for(i=1; i<=n; i++)
        d[i]=(i==sx?0:INF);

    queue<int> q;
    while(!q.empty()) q.pop();//因为要用两次,所以这个不能省略
    q.push(sx);
    v[sx]=1;
    while(!q.empty())
    {
        int x=q.front();
        q.pop();
        v[x]=0;
        for(i=1; i<=n; i++)
            if(d[i]>d[x]+w[x][i])
            {
                d[i]=d[x]+w[x][i];
                if(!v[i])
                {
                    q.push(i);
                    v[i]=1;
                }
            }
    }
}
void tran()//交换路径
{
    int i,j;
    for(i=1; i<=n; i++)
        for(j=1; j<=i; j++)
            swap(w[i][j],w[j][i]);
}
int main()
{
    int i,j,k,a,b,c,ans;
    while(~scanf("%d %d %d",&n,&m,&sx))
    {
        for(i=1; i<=n; i++)
            for(j=1; j<=n; j++)
                w[i][j]=(i==j?0:INF);

        for(i=1; i<=m; i++)
        {
            scanf("%d %d %d",&a,&b,&c);
            w[a][b]=c;
        }

        spfa(d1);
        tran();
        spfa(d2);

        for(ans=0,i=1; i<=n; i++)
            if(d1[i]!=INF&&d2[i]!=INF)
                ans=max(ans,d1[i]+d2[i]);
        printf("%d\n",ans);
    }
}

猜你喜欢

转载自blog.csdn.net/santa9527/article/details/54425028