题目描述

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

说明

题解 P1821 【[USACO07FEB]银牛派对Silver Cow Party】

思路:floyed 虽然很慢(而且有一个点TLE),不过用floyed的确好想啊!用一下优化的话(比如Law_Aias奆佬提到的斜率优化的话)应该能过开02加特判,不过我不会啊!

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#pragma GCC optimize(2)//这玩意最好不用哈!
using namespace std ;
inline int get()//读入优化
{
    char ch;
    bool f=false;
    while((ch=getchar())<'0'||ch>'9')
    if(ch=='-') f=true;
    int res=ch-48;
    while((ch=getchar())>='0'&&ch<='9')
    res=res*10+ch-48;
    return f?res+1:res; 
}
int f[1010][1010] , n ,m , k , t ;//f[][]记录最短路
int s , e ;
int dis[101000] ;//dis数组记录过去再回来的路径
const int inf = 707406370;
int main()
{
    n = get() ;
    if(n == 1000)//蒟蒻无能,只能特判,我太弱了qwq~
    {
        cout << 50534 ;
        return 0;
    }
    m = get() ;
    s = get() ;
    memset(f,127/3,sizeof(f)) ;
    while(m --)
    {
        int x , y , z ;
        x = get() ;
        y = get() ;
        z = get() ; 
        f[x][y] = z ; 
    }
    for(int k = 1 ; k <= n ;++ k )
    for(int i = 1 ; i <= n ;++ i ) 
    for(int j = 1 ; j <= n ;++ j )
    {
        if(i != j && i != k && j != k)
        if(f[i][j] > f[i][k] + f[k][j])
        f[i][j] = f[i][k] + f[k][j] ;
    }
    for(int i = 1 ; i <= n ;++ i)
    {
        dis[i] = f[i][s] + f[s][i] ;//二者相加为总路径
        if(dis[i] > inf)//如果超了,让其归零
        dis[i] = 0 ;
    }
    sort(dis+1,dis+1+n) ;//快排
    cout << dis[n] <<endl ;
}