Skip to content

Commit

Permalink
44!
Browse files Browse the repository at this point in the history
  • Loading branch information
park-jiyeong committed Aug 13, 2024
1 parent 262d655 commit 03a3027
Showing 1 changed file with 3 additions and 4 deletions.
7 changes: 3 additions & 4 deletions JYPARK/41to50/44.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import heapq
from collections import defaultdict

def solution(N, road, K):
answer = 0
Expand All @@ -12,15 +11,15 @@ def solution(N, road, K):
graph[i].append((j, weight))
graph[j].append((i, weight))

heapq.heappush(q, [1, 0]) #현재노드, 시작점에서 현재노드까지의 거리
heapq.heappush(q, [0, 1]) #시작점에서 현재노드까지의 거리, 현재노드 !heapq를 사용한 이유 -> 방문하지 않은 노드들 중 최소거리를 가지는 노드를 선택해야하므로 거리값을 튜플의 첫 번째 요소(정렬기준)로 사용해야한다.

while q:
current_node, weight = heapq.heappop(q)
weight, current_node = heapq.heappop(q)

for adjacent_node, new_weigth in graph[current_node]: #현재노드에 연결 돼있는 모든 노드를 볼 것임
if dist[adjacent_node] > dist[current_node] + new_weigth:
dist[adjacent_node] = dist[current_node] + new_weigth
heapq.heappush(q, [adjacent_node, dist[adjacent_node]])
heapq.heappush(q, [dist[adjacent_node], adjacent_node])

for key in dist:
if dist[key] <= K:
Expand Down

0 comments on commit 03a3027

Please sign in to comment.