|
| 1 | +```java |
| 2 | +import java.util.*; |
| 3 | +import java.io.*; |
| 4 | + |
| 5 | +public class boj1238 { |
| 6 | + static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 7 | + static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); |
| 8 | + static StringTokenizer st; |
| 9 | + |
| 10 | + static void nextLine() throws Exception {st = new StringTokenizer(br.readLine());} |
| 11 | + static int nextInt() {return Integer.parseInt(st.nextToken());} |
| 12 | + static void end() throws Exception {bw.flush();bw.close();br.close();} |
| 13 | + |
| 14 | + static ArrayList<ArrayList<Node>> graph = new ArrayList<>(); |
| 15 | + static int N, M, X; |
| 16 | + |
| 17 | + public static void main(String[] args) throws Exception { |
| 18 | + nextLine(); |
| 19 | + N = nextInt(); |
| 20 | + M = nextInt(); |
| 21 | + X = nextInt(); |
| 22 | + int S, E, T, answer = 0; |
| 23 | + for (int i = 0; i < N+1; i++) graph.add(new ArrayList<>()); |
| 24 | + |
| 25 | + for (int i = 0; i < M; i++) { |
| 26 | + nextLine(); |
| 27 | + S = nextInt(); |
| 28 | + E = nextInt(); |
| 29 | + T = nextInt(); |
| 30 | + graph.get(S).add(new Node(T, E)); |
| 31 | + } |
| 32 | + |
| 33 | + for (int i = 1; i < N+1; i++) { |
| 34 | + answer = Math.max(answer, dijkstra(i, X) + dijkstra(X, i)); |
| 35 | + } |
| 36 | + bw.write(answer+"\n"); |
| 37 | + end(); |
| 38 | + } |
| 39 | + |
| 40 | + static int dijkstra(int start, int target) { |
| 41 | + if (start == target) return 0; |
| 42 | + PriorityQueue<Node> pq = new PriorityQueue<>((o1, o2) -> o1.t-o2.t); |
| 43 | + int[] dist = new int[N+1]; |
| 44 | + Arrays.fill(dist, Integer.MAX_VALUE); |
| 45 | + dist[start] = 0; |
| 46 | + pq.add(new Node(0, start)); |
| 47 | + while (!pq.isEmpty()) { |
| 48 | + Node curr = pq.poll(); //시간 가장 작은 것 |
| 49 | + if (dist[curr.e] < curr.t) continue; |
| 50 | + dist[curr.e] = curr.t; |
| 51 | + for (Node n : graph.get(curr.e)) { |
| 52 | + if (n.t+dist[curr.e] >= dist[n.e]) continue; |
| 53 | + pq.add(new Node(n.t+dist[curr.e], n.e)); |
| 54 | + } |
| 55 | + } |
| 56 | + return dist[target]; |
| 57 | + } |
| 58 | + |
| 59 | + static class Node { |
| 60 | + int t; |
| 61 | + int e; |
| 62 | + public Node(int t, int e) { |
| 63 | + this.t = t; |
| 64 | + this.e = e; |
| 65 | + } |
| 66 | + } |
| 67 | +} |
| 68 | +``` |
0 commit comments