首页 > 代码库 > POJ 3268 Silver Cow Party

POJ 3268 Silver Cow Party

                                Silver Cow Party
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 13100 Accepted: 5881

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 给点 , 每个点都有一只牛。
然后一个点 goal , 表示这个点要开一个party 。
各个点中的牛要到达goal, 然后从goal返回原先的点。
哪只牛所需要的费用是最大的?
 
首先要知道 , 各个点到一个点x的最短路。其实就是构反向图,
然后在以x为源点,跑一次单源最短路 ( dij , 或者spfa ).
 
那么这个题就是正向跑一次dij , 反向一次dij 。 答案取一下各个点两个最短距离和的max即可 
 
一开始以为是无向图。纠结甚久,后来发现是有向,即水题一条了~
 
 
 
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <vector>#include <queue>using namespace std;typedef long long LL;const int N = 1010;const int M = 200010;const int inf = 1e9+7;int n , m , goal;struct node {    int v , w ;    node(){}    node( int a , int b ) { v = a  , w = b ; }    bool operator < ( const node &a ) const {        return w > a.w ;    }};vector<node> g[2][N];int dis[2][N];bool vis[N];void dij( int which ) {    priority_queue<node>que;    que.push( node( goal , 0 ) );    while( !que.empty() ) {        int u = que.top().v ,cost = que.top().w ; que.pop();        if( dis[which][u] <= cost ) continue ;        dis[which][u] = cost ;        for( int i = 0 ; i < g[which][u].size(); ++i ) {            int v = g[which][u][i].v , w = g[which][u][i].w ;            if( dis[which][u] + w < dis[which][v] ) {                que.push( node( v, dis[which][u] + w ) );            }        }    }}void init() {    for( int i = 0 ; i < 2 ; ++i ) {        for( int j = 1 ; j <= n ; ++j ) {            g[i][j].clear();            dis[i][j] = inf ;        }    }}void run(){    int u , v , w ;    init();    while( m-- ) {        scanf("%d%d%d",&u,&v,&w);        g[0][u].push_back(node(v,w));        g[1][v].push_back(node(u,w));    }    dij(0) ,dij(1);    int ans = 0 ;    for( int i = 1 ; i <= n ; ++i ) ans = max ( ans , dis[0][i] + dis[1][i] ) ;    printf("%d\n",ans);}int main(){    #ifdef LOCAL        freopen("in.txt","r",stdin);    #endif // LOCAL    int _ , cas = 1 ;    while( scanf("%d%d%d",&n,&m,&goal) != EOF ) run();}
View Code

 

POJ 3268 Silver Cow Party