본문 바로가기
알고리즘

Floyd-Warshall

by 이재현9999 2024. 10. 11.
import sys
input = sys.stdin.readline

INF = int(1e9)

n = int(input())
m = int(input())

graph = [[INF] * (n + 1) for _ in range(n + 1)]

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 = map(int, input().split())
    graph[a][b] = min(c, graph[a][b])


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(0, end=' ')
        else:
            print(graph[a][b], end=' ')
    print()

'알고리즘' 카테고리의 다른 글

Topology Sort  (0) 2024.10.11
Segment Tree  (0) 2024.10.11
Dijkstra  (0) 2024.10.11
Bellman-Ford  (0) 2024.10.11
[python] 백준 2667번 - 단지번호붙이기  (0) 2024.08.15