From ddbc3b35fcea772893e137cf802052694e1b829c Mon Sep 17 00:00:00 2001 From: suyeun84 <81475092+suyeun84@users.noreply.github.com> Date: Mon, 1 Sep 2025 23:06:08 +0900 Subject: [PATCH] =?UTF-8?q?[20250901]=20PGM=20/=20LV3=20/=20=EB=B6=80?= =?UTF-8?q?=EB=8C=80=EB=B3=B5=EA=B7=80=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 --- ...00\353\214\200\353\263\265\352\267\200.md" | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 "suyeun84/202509/01 PGM LV3 \353\266\200\353\214\200\353\263\265\352\267\200.md" diff --git "a/suyeun84/202509/01 PGM LV3 \353\266\200\353\214\200\353\263\265\352\267\200.md" "b/suyeun84/202509/01 PGM LV3 \353\266\200\353\214\200\353\263\265\352\267\200.md" new file mode 100644 index 00000000..d2ba27ea --- /dev/null +++ "b/suyeun84/202509/01 PGM LV3 \353\266\200\353\214\200\353\263\265\352\267\200.md" @@ -0,0 +1,45 @@ +```java +import java.util.*; +class Solution { + int[] answer; + int[] result; + List graph[]; + + public int[] solution(int n, int[][] roads, int[] sources, int destination) { + answer = new int[n+1]; + result = new int[sources.length]; + graph = new ArrayList[n+1]; + Arrays.fill(answer, -1); + + for (int i = 0; i < n+1; i++) { + graph[i] = new ArrayList<>(); + } + + for (int[] road : roads) { + graph[road[0]].add(road[1]); + graph[road[1]].add(road[0]); + } + + bfs(destination); + + for (int i = 0; i < sources.length; i++) { + result[i] = answer[sources[i]]; + } + return result; + } + public void bfs(int start) { + Queue q = new ArrayDeque<>(); + q.add(start); + answer[start] = 0; + while (!q.isEmpty()) { + int curr = q.poll(); + for (int next : graph[curr]) { + if (answer[next] == -1) { + answer[next] = answer[curr] + 1; + q.add(next); + } + } + } + } +} +```