標籤:des style blog http io color ar os sp
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:
N,
M, and
X
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
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