-
Notifications
You must be signed in to change notification settings - Fork 0
/
navigating.py
61 lines (46 loc) · 1.67 KB
/
navigating.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import heapq
import sys
sys.setrecursionlimit(int(1e5))
INF = int(1e3)
SPOT_NUM = 39
def path_trace(src, dst, path, temp):
if path[dst] == src:
temp.append(src)
return temp
else:
temp.append(path[dst])
path_trace(src, path[dst], path, temp)
return temp
def dijstra(src, dst):
q = []
heapq.heappush(q, (0, src))
distance[src] = 0
while q:
dist, now = heapq.heappop(q)
if dist < distance[now]:
continue
for i in graph[now]:
next, cost = i, 1+dist
if cost < distance[next]:
distance[next] = cost
heapq.heappush(q, (cost, next))
path[next] = now
temp = [dst]
result = path_trace(src, dst, path, temp)
result.reverse()
print("\npath: ", end=' ')
for x in result:
print(x, end=' ')
print()
return
if __name__ == "__main__":
graph = [[1, 13], [0, 2, 14], [1, 3, 4, 15], [2, 16], [2, 5, 17], [4, 6, 18], [5, 7, 19], [6, 8, 10, 20],\
[7, 9, 21], [8, 22], [7, 11, 23], [10, 12, 24], [11, 25], [0, 14, 26], [1, 13, 15, 27], [2, 14, 16, 17, 28],\
[3, 15, 29], [4, 15, 18, 30], [5, 17, 19, 31], [6, 18, 20, 32], [7, 19, 21, 23, 33], [8, 20, 22, 34],\
[9, 21, 35], [10, 20, 24, 36], [11, 23, 25, 37], [12, 24, 38], [13, 27], [14, 26, 28], [15, 27, 29, 30],\
[16, 28], [17, 28, 31], [18, 30, 32], [19, 31, 33], [20, 32, 34, 36], [21, 33, 35], [22, 34], [23, 33,37],\
[24, 36, 38], [25, 37]]
src, dst = map(int, input("src, dst: ").split())
distance = [INF] * SPOT_NUM
path = [0] * SPOT_NUM
dijstra(src, dst)