BOJ - 1753 - 최단경로

Updated:

import sys
from heapq import heappush, heappop

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

    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))

    for i in range(1, V+1):
        print(distance[i] if distance[i] != 1e9 else "INF")

def solution():
    V, E = map(int, sys.stdin.readline().split())
    K = int(input())
    graph = [[] for _ in range(V + 1)]

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

    Dijkstra(K, graph, V)

solution()

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

Categories:

Updated:

Leave a comment