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

    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.

    题解:

    第一个数字代表有几个N个农场,及M条路来连接这N个农场,及X表示举办party的农场求最短路的题,这个题主要是分为去和来两个过程,这就需要我们用两次Djikstra算法,求两次最小路,然后去加一下求去和的最大的,具体代码实现如下,反向回去的路要反向建图,然后就达到反向的目的

    #include<algorithm>
    #include<iostream>
    #include<cstring>
    #include<cstdio>
    
    using namespace std;
    
    const int INF=0x3f3f3f3f;
    int p[1005][1005];
    int dis1[1005];
    int dis2[1005];
    int vis[1005];
    int main()
    {
        int i,j,n,m,x,a,b,c,key,minn;
        scanf("%d%d%d",&n,&m,&x);
        for(i=1;i<=n;i++)
        {
            dis1[i]=INF;
            dis2[i]=INF;
            vis[i]=0;
            for(j=1;j<=n;j++)
                p[i][j]=INF;
        }
    
        for(i=0;i<m;i++)
        {
            scanf("%d%d%d",&a,&b,&c);
            p[a][b]=c;
        }
        dis1[x]=0;
        key=x;
        for(i=1;i<=n;i++)
        {
            minn=INF;
            for(j=1;j<=n;j++)
            {
                if(!vis[j]&&dis1[j]<minn)
                {
                    minn=dis1[j];
                    key=j;
                }
            }
            vis[key]=1;
            for(j=1;j<=n;j++)
            {
                if(minn+p[key][j]<dis1[j])
                    dis1[j]=minn+p[key][j];
            }
        }
        for(i=1;i<=n;i++)
        {
            vis[i]=0;
        }
        dis2[x]=0;
        key=x;
        for(i=1;i<=n;i++)
        {
            minn=INF;
            for(j=1;j<=n;j++)
            {
                if(!vis[j]&&dis2[j]<minn)
                {
                    minn=dis2[j];
                    key=j;
                }
            }
            vis[key]=1;
            for(j=1;j<=n;j++)
            {
                if(minn+p[j][key]<dis2[j])
                    dis2[j]=minn+p[j][key];
            }
        }
        minn=0;
        for(i=1;i<=n;i++)
        {
            if(minn<dis1[i]+dis2[i])
                minn=dis1[i]+dis2[i];
        }
        printf("%d
    ",minn);
        return 0;
    }
  • 相关阅读:
    2014多校第四场1005 || HDU 4901 The Romantic Hero (DP)
    HDU 1698 Just a Hook (线段树区间更新)
    HDU 1016 Prime Ring Problem (素数筛+DFS)
    2014多校第二场1011 || HDU 4882 ZCC Loves Codefires (贪心)
    HDU 1142 A Walk Through the Forest(SPFA+记忆化搜索DFS)
    JSP九大内置对象和四个作用域
    Jsp遍历后台传过来的List
    JavaWeb文件上传和下载
    servlet中doGet()和doPost()的区别
    Ajax请求会话过期处理(JS)
  • 原文地址:https://www.cnblogs.com/Staceyacm/p/10782096.html
Copyright © 2011-2022 走看看