diff --git a/fnzksxl/README.md b/fnzksxl/README.md index 8134ba4..cde51ea 100644 --- a/fnzksxl/README.md +++ b/fnzksxl/README.md @@ -2,5 +2,5 @@ | 차시 | 날짜 | 문제유형 | 링크 | 풀이 | |:----:|:---------:|:----:|:-----:|:----:| -| 1차시 | 2023.10.27 | BFS | - | - | +| 1차시 | 2024-01-15 | 다익스트라 | [부대 복귀](https://school.programmers.co.kr/learn/courses/30/lessons/132266) | [#2](https://github.com/AlgoLeadMe/AlgoLeadMe-6/pull/2) | --- diff --git "a/fnzksxl/\353\213\244\354\235\265\354\212\244\355\212\270\353\235\274/\353\266\200\353\214\200\353\263\265\352\267\200.py" "b/fnzksxl/\353\213\244\354\235\265\354\212\244\355\212\270\353\235\274/\353\266\200\353\214\200\353\263\265\352\267\200.py" new file mode 100644 index 0000000..d9ad7f3 --- /dev/null +++ "b/fnzksxl/\353\213\244\354\235\265\354\212\244\355\212\270\353\235\274/\353\266\200\353\214\200\353\263\265\352\267\200.py" @@ -0,0 +1,31 @@ +from heapq import heappop, heappush + +def solution(n, roads, sources, destination): + answer = [] + graph=[[] for _ in range(n+1)] + + for start, dest in roads: + graph[start].append(dest) + graph[dest].append(start) + + INF = 500001 + distances = [INF] * (n+1) + distances[destination] = 0 + + queue = [] + + heappush(queue, (distances[destination], destination)) + + while queue: + cur_dist, cur_dest = heappop(queue) + + if distances[cur_dest] < cur_dist: + continue + + for next_dest in graph[cur_dest]: + distance = cur_dist + 1 + if distance < distances[next_dest]: + distances[next_dest] = distance + heappush(queue, (distance, next_dest)) + + return [-1 if distances[s] == INF else distances[s] for s in sources] \ No newline at end of file