본문 바로가기
둥지/알고리즘

[백준 11404] 플로이드

by 까닭 2023. 6. 9.

문제링크

#include <iostream>

using namespace std;

int main()
{
    int n = 0;
    cin >> n;
    int m = 0;
    cin >> m;

    long long city[101][101] = {};

    for (size_t i = 0; i < 101; ++i)
    {
        for (size_t j = 0; j < 101; ++j)
        {
            if (i == j)
            {
                city[i][j] = 0;
                continue;
            }

            city[i][j] = INT32_MAX;
        }
    }


    for (size_t i = 0; i < m; ++i)
    {
        long long a = 0; //시작
        long long b = 0; //도착
        long long c = 0; //비용

        cin >> a >> b >> c;

        city[a][b] = min(city[a][b], c);
    }

    //플로이드 와샬 핵심 코드
    for (size_t via = 1; via <= n; ++via)
    {
        for (size_t from = 1; from <= n; ++from)
        {
            for (size_t to = 1; to <= n; ++to)
            {
                long long from_via_to = city[from][via] + city[via][to];
                long long from_to = city[from][to];

                city[from][to] = min(from_to, from_via_to);
            }
        }
    }

    for (size_t from = 1; from <= n; ++from)
    {
        for (size_t to = 1; to <= n; ++to)
        {
            if (city[from][to] >= INT32_MAX)
            {
                city[from][to] = 0;
            }

            cout << city[from][to] << " ";
        }

        cout << '\n';
    }

    return 0;
}

'둥지 > 알고리즘' 카테고리의 다른 글

[백준 15723] n단 논법  (0) 2023.07.14
[백준 15482]한글 LCS  (0) 2023.06.19
[백준 11726] 2×n 타일링  (0) 2023.05.26
[프로그래머스] 폰켓몬  (0) 2023.05.08
[프로그래머스] 프로세스  (0) 2023.05.04