-
[다익스트라 알고리즘] 최소비용 구하기Algorithms/Shortest Path 2021. 8. 13. 16:07
기출 : 백준 #1916
일반적인 다익스트라 문제이다
from heapq import heappush, heappop INF = int(1e9) n = int(input()) m = int(input()) distance = [INF for _ in range(n+1)] graph = [[] for _ in range(n+1)] for _ in range(m): x, y, w = map(int,input().split()) graph[x].append([w,y]) start, end = map(int,input().split()) def djikstra(start): queue = list() distance[start] = 0 heappush(queue, (0,start)) while queue: curVal, cur = heappop(queue) if distance[cur] < curVal: continue for nextVal, next in graph[cur]: if distance[next] > curVal + nextVal: distance[next] = curVal + nextVal heappush(queue, (curVal + nextVal, next)) djikstra(start) print(distance[end])
'Algorithms > Shortest Path' 카테고리의 다른 글
[다익스트라 알고리즘] 알고스팟 (0) 2021.08.15 [다익스트라 알고리즘] 특정한 최단 경로 (0) 2021.08.13 [다익스트라 알고리즘] 최단경로 (0) 2021.08.13 [최단 경로 알고리즘] 문제풀이 전략 #2 플로이드 워셜 알고리즘 (0) 2021.02.17 [최단 경로 알고리즘] 문제풀이 전략 # 1 다익스트라 알고리즘 (0) 2021.02.16