BOJ - 5972 - 택배 배송

Updated:

import sys
from heapq import heappush, heappop

def Dijkstra(start, graph, N):
    heap = []
    distance = [1e9] * (N +1)
    distance[start] = 0
    heappush(heap, (0, start))

    ret = [0 for _ in range(N+1)]
    while heap:
        weight, location = heappop(heap)

        if distance[location] < weight:
            continue

        for l, w in graph[location]:
            w += weight
            if w < distance[l]:
                distance[l] = w
                heappush(heap, (w, l))

    print(distance[N])

def solution():
    N, M = map(int, sys.stdin.readline().split())

    graph = [[] for _ in range(N + 1)]

    for _ in range(M):
        u, v, w = map(int, sys.stdin.readline().split())
        graph[u].append([v, w])
        graph[v].append([u, w])

    Dijkstra(1, graph, N)

solution()

https://www.acmicpc.net/problem/5972

Categories:

Updated:

Leave a comment