-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeikstra.py
More file actions
31 lines (31 loc) · 903 Bytes
/
deikstra.py
File metadata and controls
31 lines (31 loc) · 903 Bytes
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
inf = float('INf')
n, s, f = map(int, input().split())
s -= 1
f -= 1
d = [-1 for i in range(n)]
used = [False for i in range(n)]
matrix = [list(map(int, input().split())) for i in range(n)]
dist = [inf for i in range(n)]
dist[s] = 0
debug = []
for i in range(n):
index_to_use = -1
for j in range(n):
if not used[j] and (index_to_use == -1 or dist[j] < dist[index_to_use]):
index_to_use = j
for j in range(n):
if j != index_to_use and matrix[index_to_use][j] != -1:
if dist[index_to_use] + matrix[index_to_use][j] < dist[j]:
d[j] = index_to_use
dist[j] = (min(dist[j], dist[index_to_use] + matrix[index_to_use][j]))
used[index_to_use] = True
if dist[f] != inf:
path = [f + 1]
x = d[f]
while x != -1:
path.append(x + 1)
x = d[x]
# print(*reversed(path))
print(dist)
else:
print(-1)