chyam

[백준] 1916번,python - 최소비용 구하기 본문

백준

[백준] 1916번,python - 최소비용 구하기

chyam_eun 2026. 1. 13. 15:41

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

import heapq

n = int(input())
m = int(input())

graph = [[] for i in range(n+1)]

for i in range(m):
    u,v,w = map(int,input().split())
    graph[u].append([v,w])

start, end = map(int,input().split()) # 출발점, 도착점 

distance = [float('inf')] * (n+1) # 각 최소 거리 저장
distance[start] = 0

q = []
heapq.heappush(q,(0,start))

while q:
    x, y = heapq.heappop(q) # 비용, 기준점
    if distance[y] < x: # 비용이 원래보다 작다면 필요없음
        continue
    for g in graph[y]: # 그래프에 있는거 
        cost = x + g[1]
        if distance[g[0]] > cost: # 원래 비용보다 크면 갱신 
            distance[g[0]] = cost
            heapq.heappush(q,(cost,g[0]))
    
print(distance[end])