首页 > 代码库 > POJ 3268 Silver Cow Party
POJ 3268 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
Lines 2..M+1: Line i+1 describes road i with three space-separated integers: Ai, Bi, and Ti. The described road runs from farm Ai to farm Bi, requiring Ti time units to traverse.
Output
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
#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();}
POJ 3268 Silver Cow Party