POJ-3268 Silver Cow Party

Silver Cow Party

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 28088   Accepted: 12756

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

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.

题意:有n个农场,编号1~N,农场里奶牛将去X号农场。这N个农场之间有M条单向路(注意),通过第i条路将需要花费Ti单位时间。选择最短时间的最优路径来回一趟,花费在去的路上和返回农场的这些最优路径的最长时间是多少?

思路:计算出每头牛去X并且回来的最短路径所需要的时间,然后求出这n-1个农场的牛的最长时间即可,两次运用dijkstra;

1 计算回来的时间:以X为源点,求出源点到各个农场的最短路径;

2 计算去的时间:将路径反转,在用一次dij,求源点到农场的最短路径(实则求牛去X的最短路径);

3 取两次和求最大值;

/*
每只羊都要去farm X 去参加羊party
M条无向路  第i 条路经过需要 ti 时间
羊很懒 选择一条合适的花时最少的路线
返回也要选一条 跟来的时候不一样的路
这羊真是事多……
N 是 N个村庄 其实也代表n只羊
M 是有多少路
X是目的地
A->B cost Ti
*/

#include <iostream>
#include <cstring>
#include <string>
#include <cstdio>
#include <algorithm>
using namespace std;
#define inf 0x3f3f3f3f
#define N 1005
#define M 10000
int map[N][N],n,m,x,map1[N][N],vis[N],dis[M],map2[N][N];
void dijkstra(int dis[M],int map[N][N])
{
    memset(vis,0,sizeof(vis));
    for(int i=1;i<=n;i++)
    {
        dis[i]=map[x][i];
    }
    vis[x]=1;
    dis[x]=0;
    for(int i=1;i<n;i++)
    {
        int minn=inf,k=-1;
        for(int j=1;j<=n;j++)
        {
            if(!vis[j]&&dis[j]<minn)
            {
                minn=dis[j];
                k=j;
            }
        }
        vis[k]=1;
        for(int u=1;u<=n;u++)
        {
            if(!vis[u]&&dis[u]>dis[k]+map[k][u])
            dis[u]=dis[k]+map[k][u];
        }
    }
}

int main()
{
    //freopen("d.in","r",stdin);
    //freopen("d.out","w",stdout);
    cin>>n>>m>>x;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            if(i==j) {
            map1[i][j]=0;
            map2[i][j]=0;
            }
            else map1[i][j]=map1[j][i]=map2[i][j]=map2[j][i]=inf;
        }
    }
    while(m--)
    {

        int a,b,c;
        cin>>a>>b>>c;
        map1[a][b]=c;//单向的!!! bug
    }

        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
            {
                map2[i][j]=map1[j][i];
            }
        }
        /*
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
            {
                cout<<map1[i][j]<<"          ";
            }cout<<endl;
        }
        cout<<endl;

        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
            {
                cout<<map2[i][j]<<"          ";
            }cout<<endl;
        }
        cout<<endl;
        */
        int dis1[M],dis2[M];
        memset(dis1,inf,sizeof(dis1));
        memset(dis2,inf,sizeof(dis2));
        dijkstra(dis1,map1);
        dijkstra(dis2,map2);
        int maxx=-1,t1,t2;
        for(int i=1;i<=n;i++)
        {
            if((dis1[i]+dis2[i])>maxx)
            {
                maxx=dis1[i]+dis2[i];
                //t1=dis1[i];
                //t2=dis2[i];
            }
        }
        //cout<<t1<<" "<<t2<<endl;
        cout<<maxx<<endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_40046426/article/details/81103739