DevYoon

[๋ฐฑ์ค€] 1753. ์ตœ๋‹จ๊ฒฝ๋กœ (Python) ๋ณธ๋ฌธ

PS/Baekjoon

[๋ฐฑ์ค€] 1753. ์ตœ๋‹จ๊ฒฝ๋กœ (Python)

gimewn 2022. 6. 3. 13:29

link ๐Ÿ”— https://www.acmicpc.net/problem/1753

 

1753๋ฒˆ: ์ตœ๋‹จ๊ฒฝ๋กœ

์ฒซ์งธ ์ค„์— ์ •์ ์˜ ๊ฐœ์ˆ˜ V์™€ ๊ฐ„์„ ์˜ ๊ฐœ์ˆ˜ E๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. (1 ≤ V ≤ 20,000, 1 ≤ E ≤ 300,000) ๋ชจ๋“  ์ •์ ์—๋Š” 1๋ถ€ํ„ฐ V๊นŒ์ง€ ๋ฒˆํ˜ธ๊ฐ€ ๋งค๊ฒจ์ ธ ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•œ๋‹ค. ๋‘˜์งธ ์ค„์—๋Š” ์‹œ์ž‘ ์ •์ ์˜ ๋ฒˆํ˜ธ K(1 ≤ K ≤ V)๊ฐ€

www.acmicpc.net

 

๐Ÿ”ฅ ๊ธฐ๋ณธ์ ์ธ ๋‹ค์ต์ŠคํŠธ๋ผ ๋ฌธ์ œ์ธ ๊ฒƒ ๊ฐ™๋‹ค

 

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