#include <iostream> #include <vector> #include <queue> using namespace std; struct node{ int x,y,z; bool operator()(node a, node b){ return a.z>b.z; } }; vector<pair<int, int>> vec[1002]; vector<pair<int, int>> vec2[1002]; priority_queue<node, vector<node>, node> pqu; int dp[1][1002],dp2[1][1002]; int main(){ int n,m,x; scanf("%d %d %d", &n, &m, &x); for(int i=0; i<m; i++) { int a,b,c; scanf("%d %d %d", &a, &b, &c); vec[a].push_back({b,c}); vec2[b].push_back({a,c}); } int res=-1; for(int j=1; j<=n; j++) { dp[0][j]=100000001; } dp[0][x]=0; for(int j=0; j<vec2[x].size(); j++) { node t; t.x = x; t.y=vec2[x][j].first; t.z=vec2[x][j].second; pqu.push(t); } while(!pqu.empty()) { node temp = pqu.top(); pqu.pop(); if(dp[0][temp.y]>dp[0][temp.x]+temp.z) { dp[0][temp.y]=dp[0][temp.x]+temp.z; for(int j=0; j<vec2[temp.y].size(); j++) { node t; t.x = temp.y; t.y=vec2[temp.y][j].first; t.z=vec2[temp.y][j].second; pqu.push(t); } } } for(int j=1; j<=n; j++) { dp2[0][j]=100000001; } dp2[0][x]=0; for(int j=0; j<vec[x].size(); j++) { node t; t.x = x; t.y=vec[x][j].first; t.z=vec[x][j].second; pqu.push(t); } while(!pqu.empty()) { node temp = pqu.top(); pqu.pop(); if(dp2[0][temp.y]>dp2[0][temp.x]+temp.z) { dp2[0][temp.y]=dp2[0][temp.x]+temp.z; for(int j=0; j<vec[temp.y].size(); j++) { node t; t.x = temp.y; t.y=vec[temp.y][j].first; t.z=vec[temp.y][j].second; pqu.push(t); } } } for(int i=1; i<=n; i++) { res=max(res, dp[0][i]+dp2[0][i]); } cout<<res<<endl; }