Pagini recente » Cod sursa (job #3247298) | Cod sursa (job #3191155) | Cod sursa (job #360196) | Cod sursa (job #2612362) | Cod sursa (job #2954368)
#include <iostream>
#include <fstream>
int main() {
std::ifstream input("royfloyd.in");
std::ofstream output("royfloyd.out");
int graph[101][101] = {0};
int n;
input >> n;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
input >> graph[i][j];
}
}
int shortest_path[101][101] = {0};
for (auto &i: shortest_path) {
for (int &j: i) {
j = INT32_MAX;
}
}
for (int i = 1; i <= n; ++i) {
shortest_path[i][i] = 0;
}
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
if (graph[i][j]) shortest_path[i][j] = graph[i][j];
}
}
for (int k = 1; k <= n; ++k) {
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
if (shortest_path[i][k] + shortest_path[k][j] > -1) {
if (shortest_path[i][j] > shortest_path[i][k] + shortest_path[k][j]) {
shortest_path[i][j] = shortest_path[i][k] + shortest_path[k][j];
}
}
}
}
}
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
output << shortest_path[i][j] << " ";
}
output << '\n';
}
return 0;
}