본문 바로가기
Algorithm

[BAEKJOON] 1753번: 최단경로

by Y06 2021. 11. 16.

1753번 문제이다.

사진을 누르면 문제로 이동한다.

문제는 Python언어로 구현하였다.

import heapq
import sys

input = sys.stdin.readline

v, e = map(int, input().split())
start = int(input()) - 1
dist = [float('inf') for i in range(v)]
dist[start] = 0

linked = [[] for i in range(v)]
for i in range(e):
    a, b, w = map(int, input().split())
    linked[a - 1].append((w, a - 1, b - 1))

heap = []
for w, a, b in linked[start]:
    heapq.heappush(heap, (0, w, a, b))

while (heap):
    d, w, a, b = heapq.heappop(heap)
    if dist[a] + w < dist[b]:
        dist[b] = dist[a] + w
        for w, x, y in linked[b]:
            heapq.heappush(heap, (dist[b], w, x, y))

for i in dist:
    print(i if i != float('inf') else "INF")

'Algorithm' 카테고리의 다른 글

[BAEKJOON] 2739번: 구구단  (0) 2021.11.22
[BAEKJOON] 10430번: 나머지  (0) 2021.11.22
[BAEKJOON] 1450번: 냅색문제  (0) 2021.11.16
[BAEKJOON] 10451번: 순열 사이클  (0) 2021.11.08
[BAEKJOON] 2667번: 단지번호붙이기  (0) 2021.11.08