플로이드

ps
링크acmicpc.net/…
출처BOJ
문제 번호11404
문제명플로이드
레벨골드 4
분류

그래프, 전체쌍 최단경로

시간복잡도O(V^3+E)
인풋사이즈V<=100, E<=100,000
사용한 언어Python 3.11
제출기록32140KB / 204ms
최고기록204ms
해결날짜2024/03/11

코드

"""Solution code for "BOJ 11404. 플로이드".

- Problem link: https://www.acmicpc.net/problem/11404
- Solution link: http://www.teferi.net/ps/problems/boj/11404
"""

import sys
from teflib import graph as tgraph

INF = float('inf')


def main():
    n = int(sys.stdin.readline())
    m = int(sys.stdin.readline())
    mat_graph = [[INF] * n for _ in range(n)]
    for _ in range(m):
        a, b, c = [int(x) for x in sys.stdin.readline().split()]
        if c < mat_graph[a - 1][b - 1]:
            mat_graph[a - 1][b - 1] = c

    dist_mat = tgraph.all_pairs_shortest_paths(mat_graph)

    for l in dist_mat:
        print(*(0 if x == INF else x for x in l))


if __name__ == '__main__':
    main()