-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdistance_vector.py
More file actions
66 lines (59 loc) · 1.96 KB
/
Copy pathdistance_vector.py
File metadata and controls
66 lines (59 loc) · 1.96 KB
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
60
61
62
63
64
65
66
def get_input():
size = int(input())
matrix = []
for i in range(size * size):
val = input().strip()
if val == 'f':
matrix.append(float('inf'))
else:
matrix.append(int(val))
return size, matrix
def make_2d_matrix(size, matrix):
result = []
index = 0
for i in range(size):
row = []
for j in range(size):
row.append(matrix[index])
index += 1
result.append(row)
return result
def find_shortest_paths(size, graph):
answer = []
for start_node in range(size):
distances = [float('inf')] * size
distances[start_node] = 0
answer.append(distances)
for times in range(size-1):
for i in range(size):
for j in range(size):
for k in range(size):
if graph[j][k] != float('inf') and answer[i][j] != float('inf'):
if answer[i][k] > answer[i][j] + graph[j][k]:
answer[i][k] = answer[i][j] + graph[j][k]
for i in range(size):
for j in range(size):
for k in range(size):
if graph[j][k] != float('inf') and answer[i][j] != float('inf'):
if answer[i][k] > answer[i][j] + graph[j][k]:
answer[i] = ['None'] * size
break
if 'None' in answer[i]:
break
return answer
def print_answer(answer):
for i in range(len(answer)):
result = []
for x in answer[i]:
if isinstance(x, str):
result.append(x)
elif x == float('inf'):
result.append('None')
else:
result.append(str(int(x)))
print(f"Node {i}: [{', '.join(result)}]")
if __name__ == "__main__":
n, input_matrix = get_input()
graph = make_2d_matrix(n, input_matrix)
shortest_paths = find_shortest_paths(n, graph)
print_answer(shortest_paths)