# D - Silver Cow Party

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: NM, 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.

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;
#define INF 0x3f3f3f3f
struct Pair
{
int first,second;
bool friend operator < (Pair a,Pair b)
{
return a.second > b.second;
}
}pr,ne;
int n,m,l;
vector<int> edge1[1005],edge2[1005];
int length[1005][1005];
int dis1[1005],dis2[1005];

void dijkstra1()
{
memset(dis1,INF,sizeof(dis1));
bool vis[1005];
memset(vis,false,sizeof(vis));
dis1[l] = 0;
pr.first = l;
pr.second = 0;
priority_queue<Pair> Q;
Q.push(pr);
while (!Q.empty())
{
pr = Q.top();
Q.pop();

if (vis[pr.first])
continue;
vis[pr.first] = true;

for (int i = 0 ; i < edge1[pr.first].size() ; i++)
{
ne.first = edge1[pr.first][i];
ne.second = pr.second + length[pr.first][ne.first];

if (ne.second < dis1[ne.first])
{
dis1[ne.first] = ne.second;
Q.push(ne);
}
}
}
}
void dijkstra2()
{
memset(dis2,INF,sizeof(dis2));
bool vis[1005];
memset(vis,false,sizeof(vis));
dis2[l] = 0;
pr.first = l;
pr.second = 0;
priority_queue<Pair> Q;
Q.push(pr);
while (!Q.empty())
{
pr = Q.top();
Q.pop();

if (vis[pr.first])
continue;
vis[pr.first] = true;

for (int i = 0 ; i < edge2[pr.first].size() ; i++)
{
ne.first = edge2[pr.first][i];
ne.second = pr.second + length[pr.first][ne.first];

if (ne.second < dis2[ne.first])
{
dis2[ne.first] = ne.second;
Q.push(ne);
}
}
}
}

int main()
{
int i,j;
while(~scanf ("%d%d%d",&n,&m,&l))
{
memset(edge1,0,sizeof(edge1));
memset(edge2,0,sizeof(edge2));
memset(length,-1,sizeof(length));
for ( i = 1 ; i <= m ; i++)
{
int x,y,z;
scanf ("%d%d%d",&x,&y,&z);
edge1[x].push_back(y);
edge2[y].push_back(x);
if (length[x][y] == -1)
length[x][y]= z;
else
length[x][y]= min(z,length[x][y]);
}
dijkstra1();
int t;
for(i=1;i<=n;i++)
for(j=i+1;j<=n;j++)
{
t=length[i][j];
length[i][j]=length[j][i];
length[j][i]=t;
}
dijkstra2();
int lqm=0;
for(i=1;i<=n;i++)
if(lqm<(dis1[i]+dis2[i])&&i!=l)
lqm=dis1[i]+dis2[i];
printf("%d\n",lqm);
}
return 0;
}