From 6de43b3058dc3f997dde7869ac3c4807b5f24c61 Mon Sep 17 00:00:00 2001 From: LiiNi-coder <97495437+LiiNi-coder@users.noreply.github.com> Date: Sat, 30 Aug 2025 23:44:53 +0900 Subject: [PATCH] =?UTF-8?q?[20250830]=20BOJ=20/=20G5=20/=20=EA=B0=84?= =?UTF-8?q?=EC=84=A0=20=EC=9D=B4=EC=96=B4=EA=B0=80=EA=B8=B0=202=20/=20?= =?UTF-8?q?=EC=9D=B4=EC=9D=B8=ED=9D=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\354\226\264\352\260\200\352\270\260 2.md" | 81 +++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 "LiiNi-coder/202508/30 BOJ \352\260\204\354\204\240 \354\235\264\354\226\264\352\260\200\352\270\260 2.md" diff --git "a/LiiNi-coder/202508/30 BOJ \352\260\204\354\204\240 \354\235\264\354\226\264\352\260\200\352\270\260 2.md" "b/LiiNi-coder/202508/30 BOJ \352\260\204\354\204\240 \354\235\264\354\226\264\352\260\200\352\270\260 2.md" new file mode 100644 index 00000000..1e2d2307 --- /dev/null +++ "b/LiiNi-coder/202508/30 BOJ \352\260\204\354\204\240 \354\235\264\354\226\264\352\260\200\352\270\260 2.md" @@ -0,0 +1,81 @@ +```java +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.ArrayList; +import java.util.List; +import java.util.PriorityQueue; +import java.util.StringTokenizer; +import java.util.Arrays; + +public class Main { + private static BufferedReader br; + private static int n, 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 o) { + return this.w - o.w; + } + } + + public static void main(String[] args) throws IOException { + br = new BufferedReader(new InputStreamReader(System.in)); + StringTokenizer st = new StringTokenizer(br.readLine()); + n = Integer.parseInt(st.nextToken()); + m = 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 < m; 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 Node(b, c)); + graph[b].add(new Node(a, c)); + } + st = new StringTokenizer(br.readLine()); + int s = Integer.parseInt(st.nextToken()); + int t = Integer.parseInt(st.nextToken()); + + dist = new int[n + 1]; + Arrays.fill(dist, Integer.MAX_VALUE); + + dijkstra(s); + System.out.println(dist[t]); + } + + private static void dijkstra(int start) { + PriorityQueue pq = new PriorityQueue<>(); + dist[start] = 0; + pq.add(new Node(start, 0)); + + while (!pq.isEmpty()) { + Node cur = pq.poll(); + int v = cur.v; + int w = cur.w; + if (dist[v] < w) + continue; + for (Node next : graph[v]) { + if (dist[next.v] > dist[v] + next.w) { + dist[next.v] = dist[v] + next.w; + pq.add(new Node(next.v, dist[next.v])); + } + } + } + } +} + +```