Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 23716 | Accepted: 10826 |
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 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 <queue>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define INF 0x3f3f3f3f
#define FIN freopen("in.txt","r",stdin)
#define fuck(x) cout<<'['<<x<<']'<<endl
using namespace std;
typedef pair<int,int>pii;
typedef long long LL;
const int MX = 1000 + 10;
const int maxn = 1e5 + 10;
struct edge
{
int v,t;
int nxt;
}E[maxn<<1];
int head[MX],tot;
void init(){
memset(head,-1,sizeof(head));
tot = 0;
}
void add(int u,int v,int t){
E[tot].v = v;
E[tot].t = t;
E[tot].nxt = head[u];
head[u] = tot++;
}
int dis[MX],vis[MX];
void spfa(int s){
memset(dis,0x3f,sizeof(dis));
memset(vis,0,sizeof(vis));
queue<int>q;
q.push(s);
dis[s] = 0;
while(!q.empty()){
int u = q.front();q.pop();vis[u] = 0;
for(int i = head[u];~i;i = E[i].nxt){
int v = E[i].v;
if(dis[v] > dis[u] + E[i].t){
dis[v] = dis[u] + E[i].t;
if(!vis[v]){
vis[v] = 1;
q.push(v);
}
}
}
}
}
int dis1[MX];
void spfa1(int s){
memset(dis1,0x3f,sizeof(dis1));
memset(vis,0,sizeof(vis));
queue<int>q;
q.push(s);
dis1[s] = 0;
while(!q.empty()){
int u = q.front();q.pop();vis[u] = 0;
for(int i = head[u];~i;i = E[i].nxt){
int v = E[i].v;
if(dis1[v] > dis1[u] + E[i].t){
dis1[v] = dis1[u] + E[i].t;
if(!vis[v]){
vis[v] = 1;
q.push(v);
}
}
}
}
}
int n,m,x;
int u[maxn],v[maxn],t[maxn];
int main(){
//FIN;
while(~scanf("%d%d%d",&n,&m,&x)){
init();
for(int i = 0;i < m;i++){
scanf("%d%d%d",&u[i],&v[i],&t[i]);
add(u[i],v[i],t[i]);//第一次正向连边,spfa求得从X点到其它N-1个点的最短路;
}
spfa(x);
init();//删掉原来所连的边;
for(int i = 0;i < m;i++){
add(v[i],u[i],t[i]);//反向连边,spfa求得从其它N-1个点到X点的最短路;
}
spfa1(x);
int ans = 0;
for(int i = 1;i <= n;i++)
ans = max(ans,dis[i]+dis1[i]);//将两个方向的最短路径相加;
printf("%d\n",ans);
}
return 0;
}