您的位置:首页 > 其它

poj 3268 Silver Cow Party (最短路算法的变换使用 【有向图的最短路应用】 )

2017-05-08 09:46 393 查看
Silver Cow Party

Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 21805 Accepted: 9955
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.
题意:
奶牛派对:有分别来自 N 个农场的 N 头牛去农场 X 嗨皮,农场间由 M 条有向路径连接。每头牛来回都挑最短的路走,求它们走的路的最大长度?

解题思路:
源点到其他点的最短路径,其它点到源点的最短路径,用迪杰斯特拉正着找一遍,反着找一遍,求那个最大的。
#include <iostream>

#include <cstring>

using namespace std;

int  const enf=100000000;

int p[1001][1001],d[1009],s[1000],d1[1009],d2[1009],n,m;

void  r_djsk(int t)

{

    memset(d1,0,sizeof(d1));

    //集合s初始化,d[]初始化

        for(int i=0;i<=n;i++)

        {

            s[i]=false;

            d1[i]=p[i][t];

        }

        s[t]=true;d1[t]=0;

        for(int i=1;i<=n;++i)

        {

            int k=0;

            int cot=1000000;

            //在剩余点中找到一个距离他最短的点

            for(int j=1;j<=n;++j)

                if(!s[j]&&d1[j]<cot)

            {

                k=j;

                cot=d1[j];

            }

            s[k]=true;//把这个点加入这个集合

            //对所有点在更新一遍

            for(int j=1;j<=n;++j)

            {

                if(!s[j]&&(p[j][k]+d1[k])<d1[j])

                    d1[j]=p[j][k]+d1[k];

            }

        }

}

void djsk(int t)

{

     memset(d,0,sizeof(d));

     //集合s初始化,d[]初始化

        for(int i=1;i<=n;i++)

        {

            s[i]=false;

            d[i]=p[t][i];

        }

        s[t]=true;d[t]=0;

        for(int i=1;i<=n;++i)

        {

            int k=0;

            int cot=1000000;

            //在剩余点中找到一个距离他最短的点

            for(int j=1;j<=n;++j)

                if(!s[j]&&d[j]<cot)

            {

                k=j;

                cot=d[j];

            }

            s[k]=true;//把这个点加入这个集合

            //对所有点在更新一遍

            for(int j=1;j<=n;++j)

            {

                if(!s[j]&&(p[k][j]+d[k])<d[j])

                    d[j]=p[k][j]+d[k];

            }

        }

}

int main()

{

    int x;

    while(cin>>n>>m>>x)

    {

        for(int i=1;i<=1000;++i)

        for(int j=1;j<=1000;++j)

        {

            if(i==j)

                p[i][j]=0;

            else

                p[i][j]=enf;

        }

         for(int i=1;i<=m;++i)

        {

            int a,b,c;

            cin>>a>>b>>c;

            p[a][b]=c;                  //有向图时 p[b][a]=c;

        }

        djsk(x);

        r_djsk(x);

        //for(int i=1;i<=n;i++) cout<<d1[i]<<endl;

        int ma=-1;

        for(int i=1;i<=n;i++)

        {

           // cout<<d[i]<<" "<<d1[i]<<endl;

            d2[i]=d[i]+d1[i];

            if(ma<d2[i]) ma=d2[i];

        }

        cout<<ma<<endl;

    }

    return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: