둥지/알고리즘
[백준 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;
}