This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import heapq | |
INF = int(1e9) | |
N = int(input()) | |
M = int(input()) | |
graph = [[] for i in range(N+1)] | |
for i in range(M): | |
a, b, c = map(int, sys.stdin.readline().split()) | |
graph[a].append((b, c)) | |
start, arrive = map(int, input().split()) | |
dist = [INF] * (N+1) | |
def dijkstra(start): | |
q = [] | |
heapq.heappush(q, (0, start)) | |
dist[start] = 0 | |
while q: | |
d, now = heapq.heappop(q) | |
if dist[now] < d: | |
continue | |
for i in graph[now]: | |
cost = d + i[1] | |
if cost < dist[i[0]]: | |
dist[i[0]] = cost | |
heapq.heappush(q, (cost, i[0])) | |
dijkstra(start) | |
print(dist[arrive]) |
'코딩테스트 준비' 카테고리의 다른 글
Java Tree 자료구조, 순회 (0) | 2021.04.11 |
---|---|
크루스칼 알고리즘 - 최소 스패닝 트리 (0) | 2021.04.03 |
백준 알고스팟 - 우선순위 큐, 너비우선탐색 (0) | 2021.03.21 |
백준 - 유기농 배추 - bfs (0) | 2021.03.19 |
백준 최대 힙, 절댓값 힙 - heapq 사용, 파이썬 (0) | 2021.03.13 |