From 272989842d9e58b28ff0e6d7a637ea3d2bc3189d Mon Sep 17 00:00:00 2001 From: suyeun84 <81475092+suyeun84@users.noreply.github.com> Date: Thu, 9 Oct 2025 15:14:10 +0900 Subject: [PATCH] =?UTF-8?q?[20251009]=20PGM=20/=20LV2=20/=20=EB=B0=B0?= =?UTF-8?q?=EB=8B=AC=20/=20=EA=B9=80=EC=88=98=EC=97=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../09 PGM LV2 \353\260\260\353\213\254.md" | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 "suyeun84/202510/09 PGM LV2 \353\260\260\353\213\254.md" diff --git "a/suyeun84/202510/09 PGM LV2 \353\260\260\353\213\254.md" "b/suyeun84/202510/09 PGM LV2 \353\260\260\353\213\254.md" new file mode 100644 index 00000000..1198e76a --- /dev/null +++ "b/suyeun84/202510/09 PGM LV2 \353\260\260\353\213\254.md" @@ -0,0 +1,62 @@ +```java +import java.util.*; +class Solution { + static class Node implements Comparable { + int v, w; + + public Node(int v, int w) { + this.v = v; + this.w = w; + } + + @Override + public int compareTo(Node other) { + return Integer.compare(this.w, other.w); + } + } + + public int solution(int N, int[][] road, int K) { + ArrayList> graph = new ArrayList<>(); + for (int i = 0; i <= N; i++) { + graph.add(new ArrayList<>()); + } + + for (int i = 0; i < road.length; i++) { + int u = road[i][0]; + int v = road[i][1]; + int w = road[i][2]; + + graph.get(u).add(new Node(v, w)); + graph.get(v).add(new Node(u, w)); + } + + int[] dist = new int[N+1]; + Arrays.fill(dist, Integer.MAX_VALUE); + dist[1] = 0; + + PriorityQueue q = new PriorityQueue<>(); + q.add(new Node(1, 0)); + + while (!q.isEmpty()) { + Node current = q.remove(); + + if (current.w > dist[current.v]) continue; + + for (Node next : graph.get(current.v)) { + int cost = next.w + dist[current.v]; + + if (cost < dist[next.v]) { + dist[next.v] = cost; + q.add(new Node(next.v, cost)); + } + } + } + + int answer = 0; + for (int i = 1; i <= N; i++) { + if (dist[i] <= K) answer++; + } + return answer; + } +} +```