poj 3268 Silver Cow Party 【dij式的最短路】

版权声明:转载的时候记着点个赞再评论一下! https://blog.csdn.net/LOOKQAQ/article/details/82781408

题目链接:http://poj.org/problem?id=3268

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 29859   Accepted: 13549

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)

#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
const int maxn = 1e3+10;
const int inf = 0x3f3f3f3f;
int vis[maxn],dis[maxn],flag,temp[maxn];
int map1[maxn][maxn],n,m,x,a,b,t;
void dij()
{
    memset(vis,0,sizeof vis);
    for(int i=1;i<=n;i++)
    {
        if(flag == 0)
            dis[i] = map1[x][i];  //x号顶点到其余顶点
        else
            dis[i] = map1[i][x]; //其余顶点到x号顶点
    }
    vis[x] = 1;  //标记源点
    dis[x] = 0;
    int ans,next;
    for(int i=1;i<n;i++)
    {
        ans = inf;
        for(int j=1;j<=n;j++)
        {
            if(!vis[j] && dis[j] < ans)
            {
                ans = dis[j];
                next = j;
            }
        }
        vis[next] = 1;
        for(int j=1;j<=n;j++)
        {
            if(!vis[j] && dis[j] > dis[next] + map1[next][j] && flag==0)
                dis[j] = dis[next] + map1[next][j];
            else
            {
                if(!vis[j] && dis[j] > dis[next] + map1[j][next] && flag==1)
                dis[j] = dis[next] + map1[j][next];
            }
        }
    }
    if(flag ==0)
        for(int i=1;i<=n;i++)
            temp[i] = dis[i];  //用temp数组暂存一下dis
    flag = 1;
}


int main()
{
    ios::sync_with_stdio(false);
    while(cin>>n>>m>>x)
    {
        flag = 0;
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
            {
                if(i==j) map1[i][j] = 0;
                else map1[i][j] = inf;
            }
        }
        for(int i=1;i<=m;i++)
        {
            cin>>a>>b>>t;
            map1[a][b] = t;
        }
        dij();
        if(flag == 1)
            dij();
        int ans1 = -1;
        for(int i=1;i<=n;i++)
        {
            if(temp[i] + dis[i] > ans1)
                ans1 = temp[i] + dis[i];
        }
        cout<<ans1<<endl;
    }
    return 0;
}

(2)

#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
const int maxn = 1e3+10;
const int inf = 0x3f3f3f3f;
int vis[maxn],dis[maxn],dis1[maxn];
int map1[maxn][maxn],n,m,x,a,b,t;
void dij()
{
    memset(vis,0,sizeof vis);
    for(int i=1;i<=n;i++)
    {
        dis[i] = map1[x][i];  //2号顶点到其余顶点
        dis1[i] = map1[i][x]; //其余顶点到2号顶点
    }
    vis[x] = 1;  //标记源点
    int ans,next;
    for(int i=1;i<n;i++)
    {
        ans = inf;
        for(int j=1;j<=n;j++)
        {
            if(!vis[j] && dis[j] < ans)
            {
                ans = dis[j];
                next = j;
            }
        }
        vis[next] = 1;
        for(int j=1;j<=n;j++)
        {
            if(!vis[j] && dis[j] > dis[next] + map1[next][j])
                dis[j] = dis[next] + map1[next][j];
        }
    }
    memset(vis,0,sizeof vis);
    for(int i=1;i<=n;i++)
    {
        ans = inf;
        for(int j=1;j<=n;j++)
        {
            if(!vis[j] && dis1[j] < ans)
            {
                ans = dis1[j];
                next = j;
            }
        }
        vis[next] = 1;
        for(int j=1;j<=n;j++)
        {
            if(!vis[j] && dis1[j] > dis1[next] + map1[j][next]) //请仔细体会这一步
                dis1[j] = dis1[next] + map1[j][next];
        }
    }
}
int main()
{
    ios::sync_with_stdio(false);
    while(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;
                else map1[i][j] = inf;
            }
        }
        for(int i=1;i<=m;i++)
        {
            cin>>a>>b>>t;
            map1[a][b] = t;
        }
        dij();
        int ans1 = -1;
        for(int i=1;i<=n;i++)
        {
            if(dis1[i] + dis[i] > ans1)
                ans1 = dis1[i] + dis[i];
        }
        cout<<ans1<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/LOOKQAQ/article/details/82781408