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 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
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头牛,每头牛都要去一头牛家参加派对。问你其中需要走的时间最长的牛花费是多少(来回时间)。
解题思路:从一个点到其他点的最短距离很好求,只要跑一遍最短路就ok,但是要每一个点都去同一个点的话跑n-1次最短路铁定不行,时间不允许。所以采用反向边的方法,再求一次从开派对的奶牛到其他奶牛家的最短路即可。
上代码
#include<iostream>
#include<vector>
#include<stack>
#include<map>
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
#include<cmath>
using namespace std;
#define ll long long
#define rep(i,n) for(ll i = 0; i < n; i++)
#define rep2(i,start,end) for(ll i = start; i < end; i++)
#define dwn(i,n) for(int i = n; i >= 0; i--)
#define dwn2(i,start,end) for(ll i = start; i >= end; i--)
#define pll pair<ll,ll>
#define mk(x,y) make_pair(x,y)
#define pdl pair<double, ll>
const ll N = 2e3 + 200;
const ll INF = 0x3f3f3f3f;
const ll Mode = 1e9 + 7;
ll G[N][N];
ll dis[N];
ll vis[N];
ll n,m,targ;
ll dijkstra(ll start){
queue<ll> q;
for(int i = 1; i <= n; i++){
dis[i] = INF;
vis[i] = 0;
}
dis[start] = 0;
// vis[start] = 1;
q.push(start);
while(!q.empty()){
ll go = q.front();
q.pop();
// vis[go] = 1;
rep2(i,1,n+1){
if(dis[i] > dis[go] + G[go][i]){
dis[i] = dis[go] + G[go][i];
q.push(i);
}
}
}
}
int tm[N] = {0};
int main(){
while(~scanf("%lld%lld%lld",&n,&m,&targ)){
rep2(i,1,n+1){
rep2(j,1,n+1){
if(i != j)
G[i][j] = INF;
else G[i][j] = 0;
}
}
rep(i,m){
ll a,b,c;
scanf("%lld%lld%lld",&a,&b,&c);
if(G[a][b] > c) G[a][b] = c;
}
// cout<<sum<<endl;
ll sum2 = 0;
dijkstra(targ);
rep2(i,1,n+1){
// cout<<dis[i]<<" ";
tm[i] = dis[i];
}
// rep2(i,1,n+1){
// rep2(j,1,n+1){
// cout<<G[i][j]<<" ";
// }
// cout<<endl;
// }
// cout<<endl;
rep2(i,1,n+1){
rep2(j,i+1,n+1){
ll tmp = G[j][i];
G[j][i] = G[i][j];
G[i][j] = tmp;
}
}
// rep2(i,1,n+1){
// rep2(j,1,n+1){
// cout<<G[i][j]<<" ";
// }
// cout<<endl;
// }
dijkstra(targ);
rep2(i,1,n+1){
tm[i] += dis[i];
if(tm[i] > sum2) sum2 = tm[i];
// cout<<dis[i]<<" ";
}
// cout<<endl;
cout<<sum2<<endl;
}
return 0;
}