From 6dc3fc32a3cf324ced9b08b1c0d0799ef689d35c Mon Sep 17 00:00:00 2001 From: JHLEE325 <82587652+JHLEE325@users.noreply.github.com> Date: Sat, 12 Jul 2025 15:52:14 +0900 Subject: [PATCH] =?UTF-8?q?[20250712]=20BOJ=20/=20G5=20/=20=EC=B5=9C?= =?UTF-8?q?=EC=86=8C=EB=B9=84=EC=9A=A9=EA=B5=AC=ED=95=98=EA=B8=B0=20/=20?= =?UTF-8?q?=EC=9D=B4=EC=A4=80=ED=9D=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...2\251\352\265\254\355\225\230\352\270\260" | 68 +++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 "JHLEE325/202507/12 BOJ G5 \354\265\234\354\206\214\353\271\204\354\232\251\352\265\254\355\225\230\352\270\260" diff --git "a/JHLEE325/202507/12 BOJ G5 \354\265\234\354\206\214\353\271\204\354\232\251\352\265\254\355\225\230\352\270\260" "b/JHLEE325/202507/12 BOJ G5 \354\265\234\354\206\214\353\271\204\354\232\251\352\265\254\355\225\230\352\270\260" new file mode 100644 index 00000000..7ddcc249 --- /dev/null +++ "b/JHLEE325/202507/12 BOJ G5 \354\265\234\354\206\214\353\271\204\354\232\251\352\265\254\355\225\230\352\270\260" @@ -0,0 +1,68 @@ +```java +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.util.*; + +public class Main { + 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); + } + } + + public static void main(String[] args) throws Exception { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + StringTokenizer st; + StringBuilder sb = new StringBuilder(); + + int n = Integer.parseInt(br.readLine()); + int m = Integer.parseInt(br.readLine()); + + List> graph = new ArrayList<>(); + for (int i = 0; i <= n; i++) + graph.add(new ArrayList<>()); + + for (int i = 0; i < m; i++) { + st = new StringTokenizer(br.readLine()); + int from = Integer.parseInt(st.nextToken()); + int to = Integer.parseInt(st.nextToken()); + int c = Integer.parseInt(st.nextToken()); + graph.get(from).add(new Node(to, c)); + } + + st = new StringTokenizer(br.readLine()); + int start = Integer.parseInt(st.nextToken()); + int end = Integer.parseInt(st.nextToken()); + + int[] dist = new int[n + 1]; + Arrays.fill(dist, Integer.MAX_VALUE); + 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 : graph.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])); + } + } + } + System.out.println(dist[end]); + } +} +```