zoukankan      html  css  js  c++  java
  • POJ 3268 Silver Cow Party

    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个牧场里面对应住着n只奶牛,现在在农场x举行一场聚会,所有奶牛都要去参加,并且参加完聚会后返回自己的牧场,路是单向的,那么求出:去参加聚会然后再回来所用时间最长的奶牛,输出这个时间。(当然奶牛也是知道要求出最短路径的,把最短路径的最大值输出就行了,由于路不是双向的,反向再进行一次spfa)
    #include<stdio.h>
    #include<string.h>
    #include<queue>
    #include<algorithm>
    using namespace std;
    
    const int N=1010;
    const int INF=0x3f3f3f3f;
    
    int G1[N][N], G2[N][N], d1[N], d2[N], vis[N], n, x;
    
    void Init()
    {
        int i, j;
    
        for (i = 1; i <= n; i++)
        {
            d1[i] = d2[i] = INF;
            for (j = 1; j <= n; j++)
                G1[i][j] = G2[i][j] = INF;
            G1[i][i] = G2[i][i] = 0;
        }
    }
    
    void Spfa(int G[][N], int d[])
    {
        int i, v;
        queue<int>Q;
    
        memset(vis, 0, sizeof(vis));
    
        Q.push(x);
        d[x] = 0;
    
        while (!Q.empty())
        {
            v = Q.front(); Q.pop();
    
            for (i = 1; i <= n; i++)
            {
                if (d[i] > d[v]+G[v][i])
                {
                    d[i] = d[v]+G[v][i];
    
                    if (!vis[i])
                    {
                        Q.push(i);
                        vis[i] = 1;
                    }
                }
            }
    
            vis[v] = 0;
        }
    }
    
    int main ()
    {
        int m, a, b, c, M, i;
    
        while (scanf("%d%d%d", &n, &m, &x) != EOF)
        {
            Init();
            M = -INF;
    
            while (m--)
            {
                scanf("%d%d%d", &a, &b, &c);
    
                G1[a][b] = min(G1[a][b], c);
                G2[b][a] = min(G2[b][a], c);
            }
    
            Spfa(G1, d1);
            Spfa(G2, d2);
    
            for (i = 1; i <= n; i++)
                M = max(M, d1[i]+d2[i]);
    
            printf("%d
    ", M);
        }
    
        return 0;
    }
  • 相关阅读:
    Android实现摇晃手机的监听
    C#实现目录复制
    c# splitter控件使用简介
    一行代码实现java list去重
    安卓最简通讯录操作样例
    java.lang.ClassNotFoundException错误原因汇总
    在Visual Studio 2013中编译libssh2项目
    VS2013中C++创建DLL导出class类
    c#发送邮件样例
    Linux服务器挂死案例分析
  • 原文地址:https://www.cnblogs.com/syhandll/p/4810228.html
Copyright © 2011-2022 走看看