-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdijkstra.py
36 lines (31 loc) · 829 Bytes
/
dijkstra.py
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
import sys
import heapq
INF = 2 ** 30
rinput = sys.stdin.readline
rprint = sys.stdout.write
def dijkstra(src):
dist = [INF for i in range(N + 1)]
dist[src] = 0
pq = []
heapq.heappush(pq, (0, src)) # 시작점 : src
while pq:
cost, here = heapq.heappop(pq)
cost = -cost
if dist[here] < cost:
continue
for i in adj[here]:
there = i[0]
nextDist = cost + i[1]
if dist[there] > nextDist:
dist[there] = nextDist
heapq.heappush(pq, (-nextDist, there))
return dist
N = int(rinput())
M = int(rinput())
adj = [[] for i in range(N + 1)]
for i in range(M):
a, b, c = map(int, rinput().split())
adj[a].append((b, c))
fr, to = map(int, rinput().split())
cost = dijkstra(fr)[to]
print(cost)