From f69c63b451e4a1a0429dce5be518c3beaef5ca5c Mon Sep 17 00:00:00 2001 From: JHLEE325 <82587652+JHLEE325@users.noreply.github.com> Date: Thu, 17 Jul 2025 18:14:47 +0900 Subject: [PATCH] =?UTF-8?q?[20250717]=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=A4=80=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" | 91 +++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 "JHLEE325/202507/17 BOJ G4 \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/JHLEE325/202507/17 BOJ G4 \355\212\271\354\240\225\355\225\234 \354\265\234\353\213\250 \352\262\275\353\241\234.md" "b/JHLEE325/202507/17 BOJ G4 \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..3c39918e --- /dev/null +++ "b/JHLEE325/202507/17 BOJ G4 \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,91 @@ +```java +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.util.*; + +public class Main { + static int n, e; + static List> list = new ArrayList<>(); + static final int INF = 987654321; + + public static void main(String[] args) throws Exception { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + StringTokenizer st = new StringTokenizer(br.readLine()); + + n = Integer.parseInt(st.nextToken()); + e = Integer.parseInt(st.nextToken()); + + for (int i = 0; i <= n; i++) + list.add(new ArrayList<>()); + + for (int i = 0; i < e; i++) { + st = new StringTokenizer(br.readLine()); + int from = Integer.parseInt(st.nextToken()); + int to = Integer.parseInt(st.nextToken()); + int cost = Integer.parseInt(st.nextToken()); + list.get(from).add(new Node(to, cost)); + list.get(to).add(new Node(from, cost)); + } + + st = new StringTokenizer(br.readLine()); + int v1 = Integer.parseInt(st.nextToken()); + int v2 = Integer.parseInt(st.nextToken()); + + int[] dist1 = dijkstra(1); + int[] distv1 = dijkstra(v1); + int[] distv2 = dijkstra(v2); + + int path1 = dist1[v1] + distv1[v2] + distv2[n]; + int path2 = dist1[v2] + distv2[v1] + distv1[n]; + + if (dist1[v1] >= INF || distv1[v2] >= INF || distv2[n] >= INF) path1 = INF; + if (dist1[v2] >= INF || distv2[v1] >= INF || distv1[n] >= INF) path2 = INF; + + int result = Math.min(path1, path2); + + if (result >= INF) + System.out.println("-1"); + else System.out.println(result); + } + + static int[] dijkstra(int start) { + int[] dist = new int[n + 1]; + Arrays.fill(dist, INF); + dist[start] = 0; + + PriorityQueue pq = new PriorityQueue<>(); + pq.offer(new Node(start, 0)); + + while (!pq.isEmpty()) { + Node cur = pq.poll(); + int now = cur.idx; + + if (cur.cost > dist[now]) continue; + + for (Node next : list.get(now)) { + if (dist[next.idx] > dist[now] + next.cost) { + dist[next.idx] = dist[now] + next.cost; + pq.offer(new Node(next.idx, dist[next.idx])); + } + } + } + + return dist; + } + + public static class Node implements Comparable { + int idx, cost; + + Node(int idx, int cost) { + this.idx = idx; + this.cost = cost; + } + + @Override + public int compareTo(Node o) { + return Integer.compare(this.cost, o.cost); + } + } +} + +```