标签:des style blog http io color ar os sp
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
Output
Sample Input
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
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(); }
标签:des style blog http io color ar os sp
原文地址:http://www.cnblogs.com/hlmark/p/4082645.html