|
| 1 | +```java |
| 2 | +import java.io.*; |
| 3 | +import java.util.*; |
| 4 | + |
| 5 | +public class BJ_1238_파티 { |
| 6 | + |
| 7 | + private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 8 | + private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); |
| 9 | + private static StringTokenizer st; |
| 10 | + |
| 11 | + private static int N; |
| 12 | + private static int M; |
| 13 | + private static int X; |
| 14 | + |
| 15 | + private static int[] aDist; |
| 16 | + private static int[] dDist; |
| 17 | + private static List<Node>[] asc; |
| 18 | + private static List<Node>[] desc; |
| 19 | + |
| 20 | + static class Node implements Comparable<Node> { |
| 21 | + int end; |
| 22 | + int weight; |
| 23 | + |
| 24 | + public Node(int end, int weight) { |
| 25 | + this.end = end; |
| 26 | + this.weight = weight; |
| 27 | + } |
| 28 | + |
| 29 | + @Override |
| 30 | + public int compareTo(Node o) { |
| 31 | + return Integer.compare(this.weight, o.weight); |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + public static void main(String[] args) throws IOException { |
| 36 | + init(); |
| 37 | + sol(); |
| 38 | + } |
| 39 | + |
| 40 | + private static void init() throws IOException { |
| 41 | + st = new StringTokenizer(br.readLine()); |
| 42 | + |
| 43 | + N = Integer.parseInt(st.nextToken()); |
| 44 | + M = Integer.parseInt(st.nextToken()); |
| 45 | + X = Integer.parseInt(st.nextToken()); |
| 46 | + |
| 47 | + asc = new List[N + 1]; |
| 48 | + desc = new List[N + 1]; |
| 49 | + for (int i = 1; i <= N; i++) { |
| 50 | + asc[i] = new ArrayList<>(); |
| 51 | + desc[i] = new ArrayList<>(); |
| 52 | + } |
| 53 | + for (int i = 0; i < M; i++) { |
| 54 | + st = new StringTokenizer(br.readLine()); |
| 55 | + |
| 56 | + int u = Integer.parseInt(st.nextToken()); |
| 57 | + int v = Integer.parseInt(st.nextToken()); |
| 58 | + int w = Integer.parseInt(st.nextToken()); |
| 59 | + |
| 60 | + asc[u].add(new Node(v, w)); |
| 61 | + desc[v].add(new Node(u, w)); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + private static void sol() throws IOException { |
| 66 | + aDist = dijkstra(X, desc); |
| 67 | + dDist = dijkstra(X, asc); |
| 68 | + |
| 69 | + int maxTime = 0; |
| 70 | + for (int i = 1; i <= N; i++) { |
| 71 | + if (i != X) { |
| 72 | + maxTime = Math.max(maxTime, aDist[i] + dDist[i]); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + bw.write(maxTime + ""); |
| 77 | + bw.flush(); |
| 78 | + bw.close(); |
| 79 | + br.close(); |
| 80 | + } |
| 81 | + |
| 82 | + private static int[] dijkstra(int start, List<Node>[] graph) { |
| 83 | + PriorityQueue<Node> pq = new PriorityQueue<>(); |
| 84 | + |
| 85 | + int[] dist = new int[N + 1]; |
| 86 | + Arrays.fill(dist, Integer.MAX_VALUE); |
| 87 | + |
| 88 | + dist[start] = 0; |
| 89 | + pq.offer(new Node(start, 0)); |
| 90 | + |
| 91 | + while (!pq.isEmpty()) { |
| 92 | + Node current = pq.poll(); |
| 93 | + int curEnd = current.end; |
| 94 | + int curWeight = current.weight; |
| 95 | + |
| 96 | + if (curWeight > dist[curEnd]) continue; |
| 97 | + |
| 98 | + for (Node next : graph[curEnd]) { |
| 99 | + int nextEnd = next.end; |
| 100 | + int nextWeight = next.weight; |
| 101 | + |
| 102 | + int newDist = dist[curEnd] + nextWeight; |
| 103 | + |
| 104 | + if (newDist < dist[nextEnd]) { |
| 105 | + dist[nextEnd] = newDist; |
| 106 | + pq.offer(new Node(nextEnd, newDist)); |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + return dist; |
| 111 | + } |
| 112 | +} |
| 113 | +``` |
0 commit comments