BOJ - 1916 - 최소비용 구하기

Updated:

import sys
from heapq import heappush, heappop

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

    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[end])

def solution():
    N = int(input())
    M = int(input())
    graph = [[] for _ in range(N + 1)]

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

    start, end = map(int, sys.stdin.readline().split())

    Dijkstra(start, end, graph, N)

solution()

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

Categories:

Updated:

Leave a comment