zoukankan      html  css  js  c++  java
  • [kuangbin带你飞]专题四 最短路练习 D

    D - Silver Cow Party(双向边)

    题目链接:https://vjudge.net/contest/66569#problem/D

    题目:

    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.
     
    题意:有1~N个农场和每个农场的奶牛,每个奶牛需要从它当前的农场到农场编号为X的农场,
    到达目的的农场后需要再回到原来各自编号的农场,由于去和回来的权值不同,
    所以这时候需要转置邻接矩阵,我们可以换一下思路:
    以X农场为起始点,这样一个spfa下来就可以算从X出发到各个农场的最小权值和,
    然后再转置road[i][j],就可以将边反向,
    再来一个spfa就可以算去的状态了,代码如下:
    spfa算法比较快,用了94ms就AC辽
     
    //
    // Created by hanyu on 2019/7/18.
    //
    #include<iostream>
    #include<queue>
    #include<cstring>
    #include<cstdio>
    #define MAX 0x3f3f3f3f
    using namespace std;
    const int maxn=1005;
    int dgo[maxn],dback[maxn],road[maxn][maxn];
    bool book[maxn];
    int N,M,X;
    void spfa(int *d)
    {
        memset(book,false,sizeof(book));
        for(int i=1;i<=maxn;i++)
            d[i]=MAX;
        queue<int>qu;
        qu.push(X);
        book[X]= true;
        d[X]=0;
        while(!qu.empty())
        {
            int now=qu.front();
            qu.pop();
            book[now]=false;
            for(int i=1;i<=N;i++)
            {
                if(d[i]>d[now]+road[now][i])
                {
                    d[i]=d[now]+road[now][i];
                    if(!book[i])
                    {
                        qu.push(i);
                        book[i]=true;
                    }
                }
            }
        }
    }
    int main()
    {
        scanf("%d%d%d",&N,&M,&X);
        for(int i=1;i<=N;i++)
        {
            for(int j=1;j<=N;j++)
            {
                if(i==j)
                    road[i][j]=0;
                else
                    road[i][j]=MAX;
            }
        }
       
        memset(dgo,MAX,sizeof(dgo));
        memset(dback,MAX,sizeof(dback));
        int x,y,z;
        for(int i=1;i<=M;i++)
        {
            scanf("%d%d%d",&x,&y,&z);
            road[x][y]=z;
        }
        spfa(dgo);
            for(int i=1;i<=N;i++)
            for(int j=1;j<=i;j++)
                swap(road[i][j],road[j][i]);//转置邻接矩阵
            int maxx=-1;
            spfa(dback);
            for(int i=1;i<=N;i++)
            {
                maxx=max(maxx,dgo[i]+dback[i]);//X农场到其他每个农场的最小权值+其他农场到X农场的最小权值
            }
            printf("%d
    ",maxx);
        return 0;
    }
     
  • 相关阅读:
    学习MongoDB(Troubleshoot Replica Sets) 集群排除故障
    MyBatis 相同事物查询缓存问题
    Spring事物源码
    Spring Session Redis
    Tomcat配置多个域名绑定到不同项目
    Shiro相关文章资料
    一网打尽:Java 程序员必须了解的计算机底层知识!
    Chrome 80 调教篇
    谭浩强《C++程序设计》
    HTTP/HTTPS协议
  • 原文地址:https://www.cnblogs.com/Vampire6/p/11208903.html
Copyright © 2011-2022 走看看