forked from prosoftwaredevelopment/CompetetiveCodingPython
-
Notifications
You must be signed in to change notification settings - Fork 0
/
floyd.py
38 lines (33 loc) · 819 Bytes
/
floyd.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
INF = float('inf')
def printmatrix(m):
r,c = len(m),len(m[0])
for i in range(r):
for j in range(c):
print(m[i][j],end=" ")
print()
v,e = map(int,input().split())
m = [[None]*v for i in range(v)]
for i in range(v):
for j in range(v):
# src = dst
if i == j:
m[i][j] = 0
# edge doesn't exist
else:
m[i][j] = INF
# take input values
for i in range(e):
src,dst,wt = map(int,input().split())
m[src][dst] = wt
printmatrix(m)
print("-------------------")
# apply our algo
# T.C = O(v^3)
for k in range(v):
for i in range(v):
for j in range(v):
# cost of tmp path is less
# update
if m[i][k]+m[k][j] < m[i][j]:
m[i][j] = m[i][k] + m[k][j]
printmatrix(m)