
- Solved.ac 기준 : 골드 4
- 사용언어 C++
문제 풀이 및 해석
- 도시 간 최소비용 -> 플로이드-워셜 알고리즘
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
#define INF 1000000000
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, m;
cin >> n;
cin >> m;
vector<vector<int>> dist(n + 1, vector<int>(n + 1, INF));
for (int i = 1; i <= n; i++) {
dist[i][i] = 0;
}
for (int i = 0; i < m; i++) {
int a, b, c;
cin >> a >> b >> c;
dist[a][b] = min(dist[a][b], c);
}
for (int k = 1; k <= n; k++) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (dist[i][k] != INF && dist[k][j] != INF) {
dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
}
}
}
}
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
if (dist[i][j] == INF) {
cout << 0 << " ";
}
else {
cout << dist[i][j] << " ";
}
}
cout << endl;
}
return 0;
}