728x90
반응형
https://www.acmicpc.net/problem/1916
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
반응형
'Algorithm > Baekjoon' 카테고리의 다른 글
BOJ 5944 Apple Delivery Java (1) | 2024.04.12 |
---|---|
BOJ 13549 숨바꼭질 3 Python3 (0) | 2023.07.15 |
BOJ 11404 플로이드 Python3 - 플로이드 워셜 알고리즘 (0) | 2023.07.15 |
BOJ 1753 최단경로 Python3 - 다익스트라 알고리즘 (0) | 2023.07.15 |
BOJ 2606 바이러스 Python3 (0) | 2023.07.06 |