Pagini recente » Cod sursa (job #910500) | Cod sursa (job #2524382)
#include <fstream>
const std::string kInputFile = "royfloyd.in";
const std::string kOutputFile = "royfloyd.out";
const int kMaxNumNodes = 100;
int shortestPath[kMaxNumNodes][kMaxNumNodes];
int main() {
std::ifstream fin(kInputFile);
std::ofstream fout(kOutputFile);
int num_nodes;
fin >> num_nodes;
for (int i = 1; i <= num_nodes; i++) {
for (int j = 1; j <= num_nodes; j++) {
fin >> shortestPath[i][j];
}
}
for (int k = 1; k <= num_nodes; k++) {
for (int i = 1; i <= num_nodes; i++) {
for (int j = 1; j <= num_nodes; j++) {
if (i == j) {
continue;
}
if (shortestPath[i][k] == 0 ||
shortestPath[k][j] == 0) {
continue;
}
if (shortestPath[i][j] == 0) {
shortestPath[i][j] = shortestPath[i][k] +
shortestPath[k][j];
continue;
}
shortestPath[i][j] = std::min(shortestPath[i][j],
shortestPath[i][k] + shortestPath[k][j]);
}
}
}
for (int i = 1; i <= num_nodes; i++) {
for (int j = 1; j <= num_nodes; j++) {
fout << shortestPath[i][j] << ' ';
}
fout << '\n';
}
return 0;
}