zoukankan      html  css  js  c++  java
  • [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);
    }
  • 相关阅读:
    【贪心 堆】luoguP2672 推销员
    【贪心 思维题】[USACO13MAR]扑克牌型Poker Hands
    「整理」[图论]最短路系列
    收集到的小玩意儿
    初遇构造函数
    在2440开发板液晶上显示两行字
    error: converting to execution character set: Invalid or incomplete multibyte or wide character
    宽字节
    宽字符wchar_t和窄字符char区别和相互转换
    linux获取文件大小的函数
  • 原文地址:https://www.cnblogs.com/hrj1/p/11149597.html
Copyright © 2011-2022 走看看