zoukankan      html  css  js  c++  java
  • poj3268--Silver Cow Party (最短路+技巧)

    Silver Cow Party
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 16991   Accepted: 7754

    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 ≤ XN). 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

    #include <cstdio>
    #include <queue>
    #include <algorithm>
    #include <cstring>
    #define N 1010
    #define M 100010
    int d[N], D[N], v[N], G[N][N];
    int n, m , x;
    const int INF = 0x3f3f3f3f;
    using namespace std;
    
    void spfa(int *d)
    {
        for(int i=1; i<=n; i++)
        {
            v[i]=0; d[i]=INF;
        }
        d[x]=0; v[x]=1;
        queue<int> Q;
        Q.push(x);
        while(!Q.empty())
        {
            int u=Q.front(); Q.pop(); v[u]=0;
            for(int p=1; p<= n; p++)
            {
                if(d[p]>d[u]+G[u][p])
                {
                    d[p]=d[u]+G[u][p];
                    if(!v[p])
                    {
                        Q.push(p);
                        v[p]=1;
                    }    
                }    
            }    
        } 
    }
    void deal()   //矩阵的转置, 好像和反向建边一个道理 ;   
    {
        for(int i=1; i<=n; i++)
            for(int j=1; j<=i; j++)
                swap(G[i][j], G[j][i]);
    }
    int main()
    {
        while(scanf("%d%d%d", &n, &m, &x) != EOF)
        {
            memset(G, INF, sizeof(G));
            for(int i=0; i<m; i++)
            {
                int a, b, c;
                scanf("%d%d%d", &a, &b, &c);
                G[a][b]=c;
            }
            spfa(d);            //正反两次spfa() ; 
            deal();
            spfa(D);
            int re=-1;
            for(int i=1; i<=n; i++)   //去了又回来了 ;
                if(d[i] != INF && D[i] != INF)
                    re=max(re, d[i]+D[i]);
            printf("%d
    ", re);
        }
        return 0;
    } 
  • 相关阅读:
    分析ARP攻击与欺骗
    IP数据包结构
    OSI 7层模型
    PKI
    求一个字符串所有的子序列:非递归和递归算法
    空当接龙求解:java版广度优先
    mysql 解决奇葩问题续篇。
    mysql 的一个奇葩问题
    symfony 之 admin 征途二 数据库相关
    symfony 之 admin 征途一 试运行
  • 原文地址:https://www.cnblogs.com/soTired/p/5326181.html
Copyright © 2011-2022 走看看