POJ 3268 Silver Cow Party [双向最短路求最大值]

来源:互联网 发布:13米半挂车运费计算法 编辑:程序博客网 时间:2024/05/20 06:04

Description

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?

Input

Line 1: Three space-separated integers, respectively: NM, and X
Lines 2.. M+1: Line i+1 describes road i with three space-separated integers: AiBi, 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 21 2 41 3 21 4 72 1 12 3 53 1 23 4 44 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-1头牛要去x那个点 ; 求来回分别最短路的情况的和的 最大值;比较绕口,但是大意就是求两次最短路然后求最大的和;其中注意                     初始化dis 的时候我这里先按照由x到各个点,然后再把图反过来求一次各个点到x;

AC代码:

#include <iostream>#include <cstdio>#include <cmath>#include <cstring>#include <algorithm>#define inf 0Xfffffff#define info -1#define N 1005using namespace std ;int map[N][N] , vis1[N], vis2[N] , dis1[N] , dis2[N] , n , m , x ; void dijk(){int mini1 , mini2 , k1 , k2;for(int i = 1 ; i<=n ; i++){dis1[i] = map[x][i];dis2[i] = map[i][x];}for(int i = 1 ; i<=n;i++){mini1 = inf ; for(int j = 1 ; j<=n ; j++)if(!vis1[j]&&mini1>dis1[j]) mini1 = dis1[k1=j];vis1[k1] = 1 ;for(int j = 1 ; j<=n ; j++) if(!vis1[j]&&dis1[j] > map[k1][j] + dis1[k1]) dis1[j] = map[k1][j] + dis1[k1] ; }for(int i = 1 ; i<=n;i++){mini2 = inf ; for(int j = 1 ; j<=n ; j++)if(!vis2[j]&&mini2>dis2[j]) mini2 = dis2[k2=j];vis2[k2] = 1 ;for(int j = 1 ; j<=n ; j++) if(!vis2[j]&&dis2[j] > map[j][k2] + dis2[k2]) dis2[j] = map[j][k2] + dis2[k2] ; }}int main(){while(cin>>n>>m>>x){for(int i = 0 ; i <=n;i++){for(int j = 0 ; j<=n; j++){map[i][j] = inf ;}vis1[i] = 0 ;vis2[i] = 0 ;dis1[i] = inf ;dis2[i] = inf ;map[i][i] = 0 ;}for(int i = 0 ; i < m ; i++){int a , b , len ;scanf("%d%d%d",&a,&b,&len);map[a][b] = len ;}dijk();int maxi = 0 ;for(int i = 1; i<=n; i++){if(i==x) continue;if(dis1[i]+dis2[i] > maxi) maxi = dis1[i] + dis2[i];} printf("%d\n",maxi);}return 0 ;}


0 0