프로그래밍/C++
11404 플로이드 [c++]
김파츠
2023. 6. 25. 23:34
#include <iostream>
#include <vector>
#include <algorithm>
#define fastio ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define INF 1e9
#define MAX 101
using namespace std;
int N, M;
int graph[MAX][MAX];
int main(int argc, char** argv)
{
fastio
cin >> N;
cin >> M;
for (int r = 1; r <= N; r++) {
for (int c = 1; c <= N; c++) {
if (r == c)graph[r][c] = 0;
else graph[r][c] = INF;
}
}
for (int i = 0; i < M; i++) {
int a, b, cost;
cin >> a >> b >> cost;
if (graph[a][b] != 0 && graph[a][b] != INF) {
graph[a][b] = min(graph[a][b], cost);
}
else {
graph[a][b] = cost;
}
}
for (int k = 1; k <= N; k++) {
for (int r = 1; r <= N; r++) {
for (int c = 1; c <= N; c++) {
if (r == c)continue;
graph[r][c] = min(graph[r][c], graph[r][k] + graph[k][c]);
}
}
}
for (int r = 1; r <= N; r++) {
for (int c = 1; c <= N; c++) {
if (r == c || graph[r][c] == INF) cout << 0;
else cout << graph[r][c];
cout << ' ';
}
cout << '\n';
}
return 0;
}