-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
38 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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): | ||
# 도달할 수 없는 경우, 무한이라고 출력 | ||
if graph[a][b] == INF: | ||
print("무한", end = ' ') | ||
else: | ||
print(graph[a][b], end=' ') | ||
|
||
print() |