Algorithm/Baekjoon
BOJ 1916 최소비용 구하기 Python3
Bonita SY
2023. 7. 15. 15:52
728x90
https://www.acmicpc.net/problem/1916
1916번: 최소비용 구하기
첫째 줄에 도시의 개수 N(1 ≤ N ≤ 1,000)이 주어지고 둘째 줄에는 버스의 개수 M(1 ≤ M ≤ 100,000)이 주어진다. 그리고 셋째 줄부터 M+2줄까지 다음과 같은 버스의 정보가 주어진다. 먼저 처음에는 그
www.acmicpc.net
import heapq
import sys
input = sys.stdin.readline
INF = int(1e9)
N = int(input().rstrip())
M = int(input().rstrip())
graph = [[] for _ in range(N+1)]
for _ in range(M):
s, e, w = map(int, input().rstrip().split())
graph[s].append((e, w))
start, end = map(int, input().rstrip().split())
distance = [INF] * (N+1)
distance[start] = 0
def dijkstra():
q = []
heapq.heappush(q, (0, start))
while q:
weight, now = heapq.heappop(q)
if distance[now] < weight:
continue
for ce, cw in graph[now]:
cost = weight + cw
if cost < distance[ce]:
distance[ce] = cost
heapq.heappush(q, (cost, ce))
dijkstra()
print(distance[end])
728x90