• [USACO07FEB]银牛派对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 ≤ 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?

    寒假到了,N头牛都要去参加一场在编号为X(1≤X≤N)的牛的农场举行的派对(1≤N≤1000),农场之间有M(1≤M≤100000)条有向路,每条路长Ti(1≤Ti≤100)。

    每头牛参加完派对后都必须回家,无论是去参加派对还是回家,每头牛都会选择最短路径,求这N头牛的最短路径(一个来回)中最长的一条路径长度。

    输入格式

    第一行三个整数N,M, X;

    第二行到第M+1行:每行有三个整数Ai,Bi, Ti ,表示有一条从Ai农场到Bi农场的道路,长度为Ti。

    输出格式

    一个整数,表示最长的最短路得长度。

    输入输出样例

    输入 #1
    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
    
    输出 #1
    10

    说明/提示

    又是一道SPFA······

    #include<cstdio>
    #include<iostream>
    
    #define inf 0x3f3f
    
    using namespace std;
    
    int u[1000001][3],v[1000001][3];
    
    int w[1000001][3],dis[10001][3];
    
    int n,m,sx,x,y,z,hhh,i,k;
    int main()
    {
        scanf("%d%d%d",&n,&m,&sx);
        for(i=1;i<=n;i++){
            dis[i][1]=inf;
            dis[i][2]=inf;
        }
        dis[sx][1]=dis[sx][2]=0;
        for(i=1;i<=m;i++){
            scanf("%d%d%d",&x,&y,&z);
            u[i][1]=v[i][2]=x;
            v[i][1]=u[i][2]=y;
            w[i][1]=w[i][2]=z;
        }
        for(k=1;k<n;k++){
            bool pd=1;
            for(i=1;i<=m;i++){
                if(dis[v[i][1]][1]>dis[u[i][1]][1]+w[i][1]){
                    dis[v[i][1]][1]=dis[u[i][1]][1]+w[i][1];
                    pd=0;
                }
                if(pd){
                    break;
                }
            }
        }
        for(k=1;k<n;k++){
            bool pd=1;
            for(i=1;i<=m;i++){ 
                if(dis[v[i][2]][2]>dis[u[i][2]][2]+w[i][2]){
                    dis[v[i][2]][2]=dis[u[i][2]][2]+w[i][2];
                    pd=0;
                }
                   if(pd){
                    break;
                }
            }
        }
        for(i=1;i<=n;i++){ 
            hhh=max(hhh,dis[i][1]+dis[i][2]);
        } 
        printf("%d",hhh);
    }
  • 相关阅读:
    (一)主动学习概念与技术
    mybatis 分页插件PageHelper 使用方法
    单例模式-Singleton
    解决tomcat启动报 java.lang.IllegalArgumentException: Invalid <url-pattern> login in servlet mapping
    如何在MSDN上获取Win7镜像
    解决 Could not find resource com/baidou/dao/UserMapper.xml
    4、XML 配置
    3、使用Map传参 & 模糊查询
    图解python环境搭建
    2、CRUD
  • 原文地址:https://www.cnblogs.com/hrj1/p/11149597.html
Copyright © 2020-2023  润新知