정석적인 다익스트라 문제
import sys
input = sys.stdin.readline
from heapq import heappop,heappush
def dijkstra(start):
min_dists[start] = 0
heap = []
heappush(heap,[0,start])
while heap:
cur_dists, cur_node = heappop(heap)
for next_node,next_dist in graph[cur_node]:
if min_dists[next_node] > cur_dists + next_dist:
min_dists[next_node] = cur_dists + next_dist
heappush(heap,[min_dists[next_node],next_node])
N,M = map(int,input().split())
graph = {i:[] for i in range(1,N+1)}
min_dists = {i:float('inf') for i in range(1,N+1)}
for _ in range(M):
a,b,c = map(int,input().split())
graph[a].append((b,c))
graph[b].append((a,c))
dijkstra(1)
print(min_dists[N])
'practivceAlgorithm > 백준' 카테고리의 다른 글
[백준][Python] 7511 소셜네트워킹 (0) | 2021.08.05 |
---|---|
[백준][Python] 14501 퇴사 (0) | 2021.08.03 |
[백준][Python] 2042 구간합구하기 (0) | 2021.08.03 |
[백준][Python] 1561 놀이공원 (0) | 2021.08.03 |
[백준][Python] 1251 단어나누기 (0) | 2021.08.03 |