- 출발점이 정해져있어, 다익스트라를 이용하면 각 정점에 대한 최소값을 쉽게 구할 수 있기 때문에 다익스트라를 이용해 풀었다.
import heapq,sys
input = sys.stdin.readline # 시간초과를 방지하기 위한 코드
V = int(input())
M = int(input())
arr =[[]*V for _ in range(V)]
for i in range(M):
a,b,c = map(int,input().split())
arr[a - 1].append((b - 1, c))
start,end = map(int,input().split())
result = [float('INF')] * V
heap = []
def dijkstra(start):
heapq.heappush(heap,(0,start))
result[start] = 0
while heap:
sk, k = heapq.heappop(heap)
result[start] = 0
if result[k] < sk:
continue
for i in arr[k]:
cost = sk + i[1]
if cost < result[i[0]]:
result[i[0]] = cost
heapq.heappush(heap,(cost,i[0]))
dijkstra(start-1)
print(result[end-1])
'Python > 백준' 카테고리의 다른 글
[백준/Python] 16928 뱀과 사다리 게임 (0) | 2022.10.05 |
---|---|
[백준/Python] 1753 최단경로 (0) | 2022.10.01 |
[백준/Python] 13549 숨바꼭질 3 (0) | 2022.10.01 |
[백준/Python] 10026 적록색약 (1) | 2022.09.30 |
[백준/Python] 4963 섬의 개수 (1) | 2022.09.30 |