From d843ff1bc0a5dffe8dd0ec8bf5466789874a0880 Mon Sep 17 00:00:00 2001 From: LiiNi-coder <97495437+LiiNi-coder@users.noreply.github.com> Date: Wed, 3 Sep 2025 23:41:11 +0900 Subject: [PATCH] =?UTF-8?q?[20250903]=20BOJ=20/=20G5=20/=20=EC=B5=9C?= =?UTF-8?q?=EC=86=8C=EB=B9=84=EC=9A=A9=20=EA=B5=AC=ED=95=98=EA=B8=B0=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 --- ...1 \352\265\254\355\225\230\352\270\260.md" | 74 +++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 "LiiNi-coder/202509/03 BOJ \354\265\234\354\206\214\353\271\204\354\232\251 \352\265\254\355\225\230\352\270\260.md" diff --git "a/LiiNi-coder/202509/03 BOJ \354\265\234\354\206\214\353\271\204\354\232\251 \352\265\254\355\225\230\352\270\260.md" "b/LiiNi-coder/202509/03 BOJ \354\265\234\354\206\214\353\271\204\354\232\251 \352\265\254\355\225\230\352\270\260.md" new file mode 100644 index 00000000..f80e34b4 --- /dev/null +++ "b/LiiNi-coder/202509/03 BOJ \354\265\234\354\206\214\353\271\204\354\232\251 \352\265\254\355\225\230\352\270\260.md" @@ -0,0 +1,74 @@ +```java +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.PriorityQueue; +import java.util.StringTokenizer; + +public class Main { + private static BufferedReader br; + private static int n; + private static int m; + private static List[] graph; + private static int[] dist; + + private static class Node implements Comparable { + int v; + int w; + public Node(int v, int w) { + this.v = v; + this.w = w; + } + @Override + public int compareTo(Node other) { + return this.w - other.w; + } + } + + public static void main(String[] args) throws IOException { + br = new BufferedReader(new InputStreamReader(System.in)); + n = Integer.parseInt(br.readLine()); + m = Integer.parseInt(br.readLine()); + graph = new ArrayList[n + 1]; + for (int i = 1; i <= n; i++) { + graph[i] = new ArrayList<>(); + } + for (int i = 0; i < m; i++) { + StringTokenizer 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 Node(b, c)); + } + StringTokenizer st = new StringTokenizer(br.readLine()); + int start = Integer.parseInt(st.nextToken()); + int end = Integer.parseInt(st.nextToken()); + dist = new int[n + 1]; + Arrays.fill(dist, Integer.MAX_VALUE); + + dijkstra(start); + + System.out.println(dist[end]); + } + + private static void dijkstra(int start) { + PriorityQueue pq = new PriorityQueue<>(); + dist[start] = 0; + pq.add(new Node(start, 0)); + while (!pq.isEmpty()) { + Node now = pq.poll(); + if (dist[now.v] < now.w) continue; + for (Node next : graph[now.v]) { + if (dist[next.v] > dist[now.v] + next.w) { + dist[next.v] = dist[now.v] + next.w; + pq.add(new Node(next.v, dist[next.v])); + } + } + } + } +} +```