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 farmBi, 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
1 #include <cstdio> 2 #include <string.h> 3 #include <algorithm> 4 using namespace std; 5 const int INF=100000000; 6 int tu[1010][1010],vis[1010],d[2][1010]; 7 int n,m,x; 8 int ans() 9 { 10 int w=x,mins=INF; 11 memset(vis,0,sizeof(vis)); 12 for (int i=1 ;i<=n ;i++){ 13 d[0][i]=tu[x][i]; 14 d[1][i]=tu[i][x]; 15 } 16 vis[x]=1; 17 d[0][x]=0; 18 for (int i=1 ;i<=n ;i++){ 19 mins=INF; 20 for (int j=1 ;j<=n ;j++){ 21 if (!vis[j] && d[0][j]<mins ) { 22 w=j; 23 mins=d[0][j]; 24 } 25 } 26 vis[w]=1; 27 for (int j=1 ;j<=n ;j++){ 28 if (!vis[j]&& tu[w][j]!=INF && d[0][j]>d[0][w]+tu[w][j]) d[0][j]=d[0][w]+tu[w][j]; 29 } 30 } 31 memset(vis,0,sizeof(vis)); 32 vis[x]=1; 33 d[1][x]=0; 34 for (int i=1 ;i<=n ;i++){ 35 mins=INF; 36 for (int j=1 ;j<=n ;j++){ 37 if (!vis[j]&& d[1][j]<mins){ 38 w=j; 39 mins=d[1][j]; 40 } 41 } 42 vis[w]=1; 43 for (int j=1 ;j<=n ;j++){ 44 if (!vis[j] &&tu[j][w]!=INF && d[1][j]>d[1][w]+tu[j][w]) d[1][j]=d[1][w]+tu[j][w]; 45 } 46 } 47 mins=-INF,w=x; 48 for (int i=1 ;i<=n ;i++){ 49 if (d[0][i]!=INF && d[1][i]!=INF && i!=x && d[0][i]+d[1][i]>mins) { 50 mins=d[0][i]+d[1][i]; 51 w=i; 52 } 53 } 54 return (d[0][w]+d[1][w]); 55 } 56 int main() 57 { 58 while(scanf("%d%d%d",&n,&m,&x)!=EOF){ 59 for (int i=1 ;i<=n ;i++){ 60 for (int j=1 ;j<=n ;j++){ 61 tu[i][j]=INF; 62 } 63 } 64 for (int i=0 ;i<m ;i++){ 65 int a,b,c; 66 scanf("%d%d%d",&a,&b,&c); 67 tu[a][b]=c; 68 } 69 printf("%d\n",ans()); 70 } 71 return 0; 72 }