93 lines
2.5 KiB
Java
93 lines
2.5 KiB
Java
import java.util.*;
|
|
|
|
class Tuple implements Comparable<Tuple>{
|
|
int now,time;
|
|
|
|
Tuple(int now, int time) {
|
|
this.now = now;
|
|
this.time = time;
|
|
}
|
|
|
|
@Override
|
|
public int compareTo(Tuple other) {
|
|
return Integer.compare(this.time, other.time);
|
|
}
|
|
}
|
|
|
|
public class _1238 {
|
|
public static void main(String[] args) {
|
|
Scanner sc = new Scanner(System.in);
|
|
int N = sc.nextInt();
|
|
int M = sc.nextInt();
|
|
int X = sc.nextInt();
|
|
|
|
@SuppressWarnings("unchecked")
|
|
ArrayList<Tuple>[] graph = new ArrayList[N+1];
|
|
|
|
@SuppressWarnings("unchecked")
|
|
ArrayList<Tuple>[] reverse_graph = new ArrayList[N+1];
|
|
for(int i=0; i<=N; i++) {
|
|
graph[i] = new ArrayList<Tuple>();
|
|
reverse_graph[i] = new ArrayList<Tuple>();
|
|
}
|
|
|
|
for(int i=0; i<M; i++) {
|
|
int u = sc.nextInt();
|
|
int v = sc.nextInt();
|
|
int t = sc.nextInt();
|
|
|
|
graph[u].add(new Tuple(v,t));
|
|
reverse_graph[v].add(new Tuple(u, t));
|
|
}
|
|
|
|
sc.close();
|
|
|
|
PriorityQueue<Tuple> pq = new PriorityQueue<>();
|
|
|
|
// X -> N
|
|
pq.add(new Tuple(X, 0));
|
|
int[] coming_time = new int[N+1];
|
|
Arrays.fill(coming_time, Integer.MAX_VALUE);
|
|
coming_time[X] = 0;
|
|
|
|
while(!pq.isEmpty()) {
|
|
Tuple current = pq.poll();
|
|
|
|
if(current.time > coming_time[current.now]) continue;
|
|
|
|
for (Tuple next : graph[current.now]) {
|
|
if(coming_time[next.now] > current.time + next.time) {
|
|
coming_time[next.now] = current.time + next.time;
|
|
pq.add(new Tuple(next.now, coming_time[next.now]));
|
|
}
|
|
}
|
|
}
|
|
|
|
// N -> X
|
|
pq.add(new Tuple(X, 0));
|
|
int[] going_time = new int[N+1];
|
|
Arrays.fill(going_time, Integer.MAX_VALUE);
|
|
going_time[X] = 0;
|
|
|
|
while(!pq.isEmpty()) {
|
|
Tuple current = pq.poll();
|
|
|
|
if(current.time > going_time[current.now]) continue;
|
|
|
|
for (Tuple next : reverse_graph[current.now]) {
|
|
if(going_time[next.now] > current.time + next.time) {
|
|
going_time[next.now] = current.time + next.time;
|
|
pq.add(new Tuple(next.now, going_time[next.now]));
|
|
}
|
|
}
|
|
}
|
|
|
|
int maxTime = 0;
|
|
for(int i=1; i<=N; i++) {
|
|
maxTime = Math.max(maxTime, going_time[i] + coming_time[i]);
|
|
}
|
|
|
|
System.out.println(maxTime);
|
|
}
|
|
}
|