Skip to content

Commit

Permalink
[Add] 플로이드 워셜 알고리즘 (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
heerucan committed Mar 10, 2022
1 parent b9f5e62 commit ef7f660
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
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()
Empty file.

0 comments on commit ef7f660

Please sign in to comment.