반응형
https://www.acmicpc.net/problem/1916
아주 기본적인 다익스트라문제다.
이런 문제를 고민하면 안된다.
import collections
import heapq
n = int(input())
m = int(input())
graph = collections.defaultdict(list)
for _ in range(m):
u,v,w = map(int, input().split())
graph[u].append([v,w])
start, fin = map(int, input().split())
dist = {}
def dijkstra(node):
q = [[0,node]]
while q:
cost, cur = heapq.heappop(q)
if cur not in dist:
dist[cur] = cost
for next, w in graph[cur]:
heapq.heappush(q, [cost+w, next])
dijkstra(start)
print(dist[fin])
댓글