-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdjikstra_hr.py
42 lines (28 loc) · 992 Bytes
/
djikstra_hr.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
37
38
39
40
41
from collections import defaultdict
from heapq import heappop, heappush, heapify
def djikstra(n, graph, start):
hq = [(0, start)]
heapify(hq)
dist = [-1]*n
while hq:
weight, node = heappop(hq)
if dist[node-1] != -1:
continue
dist[node-1] = weight
for neigh, w in graph[node].items():
if dist[neigh-1] == -1:
heappush(hq, (weight+w, neigh))
dist.pop(start-1)
return dist
if __name__ == "__main__":
for _ in range(int(input())):
n, m = map(int, input().split())
graph = defaultdict(lambda : defaultdict(lambda : 10**8))
# Le test 7 ne passe pas en temps si on ne gère pas les arêtes en doublon...
# Dure vie
for _ in range(m):
a, b, d = map(int, input().split())
graph[a][b] = min(graph[a][b], d)
graph[b][a] = min(graph[b][a], d)
start = int(input())
print(*djikstra(n, graph, start))