[프로그래머스] 배달 (python 파이썬)

코딩하는계란·2021년 8월 16일
0

프로그래머스

목록 보기
16/16
post-thumbnail

👉 배달



✍ 내 코드


from collections import defaultdict
import heapq as hq


def solution(N, road, K):
    dic = defaultdict(set)
    gp = [float("inf")] * (N + 1)
    for s, e, w in road:
        dic[s].add((e, w))
        dic[e].add((s, w))

    que = []
    hq.heappush(que, (0, 1))
    gp[1] = 0
    while que:
        cur_dis, node = hq.heappop(que)
        for e, w in dic[node]:
            if gp[e] > cur_dis + w:
                gp[e] = cur_dis + w
                hq.heappush(que, (gp[e], e))

    return len(list(filter(lambda x: x <= K, gp)))


✍ 팁


  • 다익스트라 알고리즘에 충실한 문제
profile
코딩💻 고양이😺

0개의 댓글