-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1916.py
More file actions
65 lines (49 loc) · 1.43 KB
/
1916.py
File metadata and controls
65 lines (49 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# 최소비용 구하기
# from collections import deque
# N = int(input())
# M = int(input())
# busMap = [[-1]*(N+1) for _ in range(N+1)]
# station = [1e9] * (N+1)
# for _ in range(M):
# a, b, weight = map(int, input().split())
# busMap[a][b] = weight
# start, end = map(int, input().split())
# queue = deque()
# station[start] = 0
# queue.append(start)
# while queue:
# vertex = queue.popleft()
# if vertex == end:
# continue
# for i in range(len(busMap[vertex])):
# if busMap[vertex][i] != -1:
# queue.append(i)
# station[i] = min(station[vertex]+busMap[vertex][i], station[i])
# print(station[end])
# ====
# pypy3 : 메모리 초과
# python3 : 시간 초과
#
# 인접 행렬 -> 인접 리스트 변경
# 큐 -> 우선순위 큐 & heap 변경
import heapq
N = int(input())
M = int(input())
busList = [[] for _ in range(N+1)]
station = [1e9] * (N+1)
for _ in range(M):
a, b, weight = map(int, input().split())
busList[a].append([weight, b])
start, end = map(int, input().split())
station[start] = 0
queue = [(station[start], start)]
while queue:
weight, now = heapq.heappop(queue)
if station[now] < weight:
continue
for nextStation in busList[now]:
cost = weight + nextStation[0]
if station[nextStation[1]] > cost:
station[nextStation[1]] = cost
heapq.heappush(queue, (cost, nextStation[1]))
print(station[end])