코딩테스트/프로그래머스

[다익스트라][Heap] 배달

박소민 2023. 12. 17. 03:51
배달
 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

 

 

import heapq

def dijkstra(start, graph, dist):
    queue=[]
    heapq.heappush(queue, (dist[start], start))
    
    while queue:
        cost, node= heapq.heappop(queue)
        for c, n in graph[node]:
            if cost+c>=dist[n]: continue
            dist[n]=cost+c
            heapq.heappush(queue, (dist[n], n))
        

def solution(N, road, K):
    answer = 0
    dist=[float('inf') for _ in range(N+1)]
    dist[1]=0
    graph=[[] for _ in range(N+1)]

    for rd in road:
        graph[rd[0]].append((rd[2], rd[1])) #거리, 도착지
        graph[rd[1]].append((rd[2], rd[0]))
    
    dijkstra(1, graph, dist)

    answer=len([d for d in dist if d<=K])
    return answer

'코딩테스트 > 프로그래머스' 카테고리의 다른 글

[그리디] 마법의 엘리베이터  (0) 2024.01.11
[Union-Find][BFS] 전력망을 둘로 나누기  (1) 2024.01.04
[BFS] 무인도 여행  (1) 2023.12.17
[Heap][해시] 베스트 앨범  (1) 2023.12.10
[DFS] 타겟 넘버  (0) 2023.12.03