forked from ndb796/python-for-coding-test
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path3.py
36 lines (31 loc) Β· 1.22 KB
/
3.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
INF = int(1e9) # 무νμ μλ―Ένλ κ°μΌλ‘ 10μ΅μ μ€μ
# λ
Έλμ κ°μ λ° κ°μ μ κ°μλ₯Ό μ
λ ₯λ°κΈ°
n = int(input())
m = int(input())
# 2μ°¨μ 리μ€νΈ(κ·Έλν νν)λ₯Ό λ§λ€κ³ , λͺ¨λ κ°μ 무νμΌλ‘ μ΄κΈ°ν
graph = [[INF] * (n + 1) for _ in range(n + 1)]
# μκΈ° μμ μμ μκΈ° μμ μΌλ‘ κ°λ λΉμ©μ 0μΌλ‘ μ΄κΈ°ν
for a in range(1, n + 1):
for b in range(1, n + 1):
if a == b:
graph[a][b] = 0
# κ° κ°μ μ λν μ 보λ₯Ό μ
λ ₯ λ°μ, κ·Έ κ°μΌλ‘ μ΄κΈ°ν
for _ in range(m):
# Aμμ Bλ‘ κ°λ λΉμ©μ CλΌκ³ μ€μ
a, b, c = map(int, input().split())
graph[a][b] = c
# μ νμμ λ°λΌ νλ‘μ΄λ μμ
μκ³ λ¦¬μ¦μ μν
for k in range(1, n + 1):
for a in range(1, n + 1):
for b in range(1, n + 1):
graph[a][b] = min(graph[a][b], graph[a][k] + graph[k][b])
# μνλ κ²°κ³Όλ₯Ό μΆλ ₯
for a in range(1, n + 1):
for b in range(1, n + 1):
# λλ¬ν μ μλ κ²½μ°, 무ν(INFINITY)μ΄λΌκ³ μΆλ ₯
if graph[a][b] == 1e9:
print("INFINITY", end=" ")
# λλ¬ν μ μλ κ²½μ° κ±°λ¦¬λ₯Ό μΆλ ₯
else:
print(graph[a][b], end=" ")
print()