반응형
https://www.acmicpc.net/problem/1916
1916번: 최소비용 구하기
첫째 줄에 도시의 개수 N(1 ≤ N ≤ 1,000)이 주어지고 둘째 줄에는 버스의 개수 M(1 ≤ M ≤ 100,000)이 주어진다. 그리고 셋째 줄부터 M+2줄까지 다음과 같은 버스의 정보가 주어진다. 먼저 처음에는 그
www.acmicpc.net
아주 기본적인 다익스트라문제다.
이런 문제를 고민하면 안된다.
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])
댓글