https://www.acmicpc.net/problem/1238
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.PriorityQueue;
import java.util.StringTokenizer;
public class Main {
static int N, M, X;
static HashMap<Integer, ArrayList<Edge>> map;
static void input() {
Reader scanner = new Reader();
N = scanner.nextInt();
M = scanner.nextInt();
X = scanner.nextInt();
map = new HashMap<Integer, ArrayList<Edge>>();
for(int vertex = 1; vertex <= N; vertex++) map.put(vertex, new ArrayList<Edge>());
for(int index = 0; index < M; index++) {
int start = scanner.nextInt();
int end = scanner.nextInt();
int weight = scanner.nextInt();
map.get(start).add(new Edge(end, weight));
}
}
static void solution() {
int[][] distances = new int[N + 1][N + 1];
for(int vertex = 1; vertex <= N; vertex++) {
dijkstra(vertex, distances[vertex]);
}
int max = Integer.MIN_VALUE;
for(int vertex = 1; vertex <= N; vertex++) {
int distance = distances[vertex][X] + distances[X][vertex];
max = Math.max(max, distance);
}
System.out.println(max);
}
static void dijkstra(int start, int[] distance) {
PriorityQueue<Edge> queue = new PriorityQueue<Edge>();
queue.offer(new Edge(start, 0));
Arrays.fill(distance, Integer.MAX_VALUE);
distance[start] = 0;
while(!queue.isEmpty()) {
Edge curEdge = queue.poll();
if(distance[curEdge.vertex] < curEdge.weight) continue;
for(Edge e : map.get(curEdge.vertex)) {
if(distance[e.vertex] > e.weight + distance[curEdge.vertex]) {
distance[e.vertex] = e.weight + distance[curEdge.vertex];
queue.offer(new Edge(e.vertex, distance[e.vertex]));
}
}
}
}
public static void main(String[] args) {
input();
solution();
}
static class Reader {
BufferedReader br;
StringTokenizer st;
public Reader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while(st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch(IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
}
static class Edge implements Comparable<Edge> {
int vertex, weight;
public Edge(int vertex, int weight) {
this.vertex = vertex;
this.weight = weight;
}
public int compareTo(Edge e) {
if(this.weight != e.weight) {
return this.weight - e.weight;
} else {
return this.vertex - e.vertex;
}
}
}
}