DevYoon
[๋ฐฑ์ค] 1753. ์ต๋จ๊ฒฝ๋ก (Python) ๋ณธ๋ฌธ
link ๐ https://www.acmicpc.net/problem/1753
๐ฅ ๊ธฐ๋ณธ์ ์ธ ๋ค์ต์คํธ๋ผ ๋ฌธ์ ์ธ ๊ฒ ๊ฐ๋ค
import heapq
import sys
V, E = map(int, sys.stdin.readline().split())
snum = int(sys.stdin.readline())
board = [[] for _ in range(V+1)]
result = [2e18]*(V+1)
def dijkstra(start):
heap = []
result[start] = 0
heapq.heappush(heap, (0, start)) # ๋น์ฉ, ๊ฒฝ์ ์ง
while heap:
kycost, ky = heapq.heappop(heap)
if kycost > result[ky]:
continue
for b in board[ky]:
gokycost = kycost+b[0]
if gokycost < result[b[1]]:
result[b[1]] = gokycost
heapq.heappush(heap, (gokycost, b[1]))
for _ in range(E):
u, v, w = map(int, sys.stdin.readline().split())
board[u].append((w, v)) # ๋น์ฉ, ๋์ฐฉ์ง
dijkstra(snum)
for idx in range(1, V+1):
if result[idx] == 2e18:
print('INF')
else:
print(result[idx])