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;
    }
  • 相关阅读:
    Perl如何安装新模块/包
    Perl入门(二)Perl的流程控制
    Perl入门(一)Perl的基本类型及运算符
    Struts2表单数据接收方式
    Struts2 自定义拦截器时Action无法接收到参数
    深入理解Java闭包概念
    Centos 7.2 Jenkins+Ansible+Gitlab 部署maven项目
    Centos 7.2 Jenkins+Ansible+Gitlab 基础配置
    CentOS 7.2 搭建Jenkins
    Linux系统上安装配置MAVEN
  • 原文地址:https://www.cnblogs.com/Staceyacm/p/10782096.html
Copyright © 2011-2022 走看看