From e1078e6eb2b209adba0fd7ea541d06386daa9dcf Mon Sep 17 00:00:00 2001 From: Ukj0ng <90972240+Ukj0ng@users.noreply.github.com> Date: Thu, 28 Aug 2025 09:17:27 +0900 Subject: [PATCH] =?UTF-8?q?[20250828]=20BOJ=20/=20G4=20/=20=EB=91=90=20?= =?UTF-8?q?=EB=8B=A8=EA=B3=84=20=EC=B5=9C=EB=8B=A8=20=EA=B2=BD=EB=A1=9C=20?= =?UTF-8?q?1=20/=20=ED=95=9C=EC=A2=85=EC=9A=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...353\213\250 \352\262\275\353\241\234 1.md" | 95 +++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 "Ukj0ng/202508/28 BOJ G4 \353\221\220 \353\213\250\352\263\204 \354\265\234\353\213\250 \352\262\275\353\241\234 1.md" diff --git "a/Ukj0ng/202508/28 BOJ G4 \353\221\220 \353\213\250\352\263\204 \354\265\234\353\213\250 \352\262\275\353\241\234 1.md" "b/Ukj0ng/202508/28 BOJ G4 \353\221\220 \353\213\250\352\263\204 \354\265\234\353\213\250 \352\262\275\353\241\234 1.md" new file mode 100644 index 00000000..53ff8c81 --- /dev/null +++ "b/Ukj0ng/202508/28 BOJ G4 \353\221\220 \353\213\250\352\263\204 \354\265\234\353\213\250 \352\262\275\353\241\234 1.md" @@ -0,0 +1,95 @@ +``` +import java.io.*; +import java.util.*; + +public class Main { + private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); + private static final long INF = (long)2e10 + 10; + private static List[] graph; + private static long[] dist; + private static int N, M, X, Y, Z; + + public static void main(String[] args) throws IOException { + init(); + + dijkstra(X, 0); + long result = dist[Y]; + dijkstra(Y, 0); + result += dist[Z]; + if (result > INF) { + bw.write("-1 "); + } else { + bw.write(result + " "); + } + + dijkstra(X, Y); + if (dist[Z] == INF) { + bw.write("-1 "); + } else { + bw.write(dist[Z] + " "); + } + + bw.flush(); + bw.close(); + br.close(); + } + + private static void init() throws IOException { + StringTokenizer st = new StringTokenizer(br.readLine()); + N = Integer.parseInt(st.nextToken()); + M = Integer.parseInt(st.nextToken()); + + graph = new List[N+1]; + dist = new long[N+1]; + for (int i = 1; i <= N; i++) { + graph[i] = new ArrayList<>(); + } + + for (int i = 0; i < M; i++) { + st = new StringTokenizer(br.readLine()); + int a = Integer.parseInt(st.nextToken()); + int b = Integer.parseInt(st.nextToken()); + int cost = Integer.parseInt(st.nextToken()); + + graph[a].add(new Node(b, cost)); + } + + st = new StringTokenizer(br.readLine()); + X = Integer.parseInt(st.nextToken()); + Y = Integer.parseInt(st.nextToken()); + Z = Integer.parseInt(st.nextToken()); + } + + private static void dijkstra(int start, int stopover) { + PriorityQueue pq = new PriorityQueue<>((o1, o2) -> Long.compare(o1.cost, o2.cost)); + Arrays.fill(dist, INF); + dist[start] = 0; + pq.add(new Node(start, 0)); + + while (!pq.isEmpty()) { + Node current = pq.poll(); + + if (current.cost > dist[current.dest]) continue; + + for (Node next : graph[current.dest]) { + if (next.dest == stopover) continue; + if (current.cost + next.cost < dist[next.dest]) { + dist[next.dest] = current.cost + next.cost; + pq.add(new Node(next.dest, dist[next.dest])); + } + } + } + } + + static class Node { + int dest; + long cost; + + Node(int dest, long cost) { + this.dest = dest; + this.cost = cost; + } + } +} +```