#include<iostream>
#include<queue>
#include<vector>
using namespace std;
int N, M, X;
vector<pair<int,int>> node[2][1010];
int arr[2][1010];

void dijk(int a) {
	priority_queue<pair<int, int>> pq;
	arr[a][X] = 0;
	
	pq.push({ 0,X });
	while (!pq.empty()) {
		int cost = -pq.top().first;
		int here = pq.top().second;
		pq.pop();

		if (arr[a][here] < cost) continue;


		for (int i = 0; i < node[a][here].size(); i++) { // 경유가 더 싼지 확인
			int via_cost = node[a][here][i].second + cost;
			if (via_cost < arr[a][node[a][here][i].first]) {
				arr[a][node[a][here][i].first] = via_cost;
				pq.push({ -via_cost , node[a][here][i].first });

			}
			

		}




	}


}
int main() {
	cin >> N >> M >> X;
	for (int i = 1; i <= N; i++) {
		for (int j = 0; j <= 1; j++) {
			arr[j][i] = 987654321;
		}
			
		
	}


	for (int i = 1; i <= M; i++) {
		int a, b, ti;

		cin >> a >> b >> ti;

		node[0][a].push_back({ b,ti });
		node[1][b].push_back({ a,ti }); //역방향 만들어주기 

	}

	dijk(0); //정방향


	dijk(1); //역방향
	int max_cost = 0;
	int tmp = 0;
	for (int i = 1; i <= N; i++) {
		if (arr[0][i] + arr[1][i] > max_cost) {
			max_cost = arr[0][i] + arr[1][i];


		}

	}
	cout << max_cost;
	return 0;
}

+ Recent posts