Pagini recente » Cod sursa (job #2155997) | Monitorul de evaluare | Cod sursa (job #243719) | Cod sursa (job #3342401) | Cod sursa (job #3357802)
#include <iostream>
#include <fstream>
#include <climits>
#include <algorithm>
int main() {
std::ifstream input("royfloyd.in");
std::ofstream output("royfloyd.out");
int n;
input >> n;
int graph[101][101];
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
input >> graph[i][j];
if (i != j && graph[i][j] == 0) {
graph[i][j] = INT_MAX;
}
}
}
for (int k = 1; k <= n; ++k) {
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
if (graph[i][k] != INT_MAX && graph[k][j] != INT_MAX) {
graph[i][j] = std::min(graph[i][j], graph[i][k] + graph[k][j]);
}
}
}
}
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
if (graph[i][j] == INT_MAX) {
output << "0 ";
} else {
output << graph[i][j] << " ";
}
}
output << '\n';
}
return 0;
}