Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 17017 | Accepted: 7767 |
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<stdio.h> #include<string.h> #include<queue> #include<cstdio> #include<string> #include<math.h> #include<algorithm> #define LL long long #define PI atan(1.0)*4 #define DD double #define MAX 110000 #define mod 100 #define dian 1.000000011 #define INF 0x3f3f3f using namespace std; int head[MAX],ans; int n,m,j,i,t,k,x; int a,b,c; int vis[MAX],dis[MAX]; struct node { int u,v,w; int next; }edge[MAX]; void add(int u,int v,int w) { edge[ans].u=u; edge[ans].v=v; edge[ans].w=w; edge[ans].next=head[u]; head[u]=ans++; } void getmap() { memset(head,-1,sizeof(head)); ans=0; for(i=1;i<=m;i++) { scanf("%d%d%d",&a,&b,&c); add(a,b,c); } } int spfa(int sx,int sy) { int i,j; queue<int>q; memset(vis,0,sizeof(vis)); for(i=1;i<=n;i++) dis[i]=INF; vis[sx]=1; dis[sx]=0; q.push(sx); while(!q.empty()) { int u=q.front(); q.pop(); vis[u]=0; for(i=head[u];i!=-1;i=edge[i].next) { int top=edge[i].v; if(dis[top]>dis[u]+edge[i].w) { dis[top]=dis[u]+edge[i].w; if(!vis[top]) { vis[top]=1; q.push(top); } } } } return dis[sy]; } void solve() { int Max=-INF; int Min=INF; int ant; for(i=1;i<=n;i++) { ant=spfa(i,x)+spfa(x,i); Max=max(Max,ant); } printf("%d\n",Max); } int main() { while(scanf("%d%d%d",&n,&m,&x)!=EOF) { getmap(); solve(); } return 0; }