From 13297a2991e0876395b556a9aeed302570fd8f79 Mon Sep 17 00:00:00 2001 From: LiiNi-coder <97495437+LiiNi-coder@users.noreply.github.com> Date: Sat, 20 Dec 2025 23:43:40 +0900 Subject: [PATCH] =?UTF-8?q?[20251220]=20BOJ=20/=20G4=20/=20=ED=8A=B9?= =?UTF-8?q?=EC=A0=95=ED=95=9C=20=EC=B5=9C=EB=8B=A8=20=EA=B2=BD=EB=A1=9C=20?= =?UTF-8?q?/=20=EC=9D=B4=EC=9D=B8=ED=9D=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...4\353\213\250 \352\262\275\353\241\234.md" | 80 +++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 "LiiNi-coder/202512/20 BOJ \355\212\271\354\240\225\355\225\234 \354\265\234\353\213\250 \352\262\275\353\241\234.md" diff --git "a/LiiNi-coder/202512/20 BOJ \355\212\271\354\240\225\355\225\234 \354\265\234\353\213\250 \352\262\275\353\241\234.md" "b/LiiNi-coder/202512/20 BOJ \355\212\271\354\240\225\355\225\234 \354\265\234\353\213\250 \352\262\275\353\241\234.md" new file mode 100644 index 00000000..9d99e36f --- /dev/null +++ "b/LiiNi-coder/202512/20 BOJ \355\212\271\354\240\225\355\225\234 \354\265\234\353\213\250 \352\262\275\353\241\234.md" @@ -0,0 +1,80 @@ +```java +import java.io.*; +import java.util.*; + +public class Main { + static class Edge { + int to; + int cost; + Edge(int to, int cost){ + this.to = to; + this.cost = cost; + } + } + + static final int INF = 100_000_000; + static ArrayList[] Graph; + + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + StringTokenizer st = new StringTokenizer(br.readLine()); + int N = Integer.parseInt(st.nextToken()); + int E = Integer.parseInt(st.nextToken()); + + Graph = new ArrayList[N + 1]; + for(int i = 1; i <= N; i++){ + Graph[i] = new ArrayList<>(); + } + for(int i = 0; i < E; i++){ + st = new StringTokenizer(br.readLine()); + int a = Integer.parseInt(st.nextToken()); + int b = Integer.parseInt(st.nextToken()); + int c = Integer.parseInt(st.nextToken()); + Graph[a].add(new Edge(b, c)); + Graph[b].add(new Edge(a, c)); + } + st = new StringTokenizer(br.readLine()); + int A = Integer.parseInt(st.nextToken()); + int B = Integer.parseInt(st.nextToken()); + int[] dist1 = dikstra(1, N); + int[] distA = dikstra(A, N); + int[] distB = dikstra(B, N); + + long path1 = (long)dist1[A] + distA[B] + distB[N]; + long path2 = (long)dist1[B] + distB[A] + distA[N]; + long answer = Math.min(path1, path2); + System.out.println((answer >= INF) ? -1 : answer); + } + + private static int[] dikstra(int start, int N){ + int[] dist = new int[N + 1]; + Arrays.fill(dist, INF); + dist[start] = 0; + + PriorityQueue pq = new PriorityQueue<>( + (a, b) -> a[1] - b[1] + ); + pq.offer(new int[]{start, 0}); + + while(!pq.isEmpty()){ + int[] cur = pq.poll(); + int now = cur[0]; + int cost = cur[1]; + + if(cost > dist[now]) + continue; + + for(Edge e : Graph[now]){ + int next = e.to; + int nextCost = cost + e.cost; + if(nextCost < dist[next]){ + dist[next] = nextCost; + pq.offer(new int[]{next, nextCost}); + } + } + } + return dist; + } +} + +```