Pagini recente » Cod sursa (job #1456572) | Cod sursa (job #1466947) | Cod sursa (job #1824768) | Cod sursa (job #1721700) | Cod sursa (job #2241776)
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <vector>
#include <array>
#include <algorithm>
#include <vector>
#include <assert.h>
using LL = long long;
using ULL = int long long;
const std::string _problemName = "royfloyd";
namespace std {
std::ifstream fin(_problemName + ".in");
std::ofstream fout(_problemName + ".out");
}
#define cin fin
#define cout fout
int main() {
int n;
std::cin >> n;
std::vector < std::vector<int> > adjMatrix(n, std::vector<int>(n));
for (int line = 0; line < n; ++line) {
for (int col = 0; col < n; ++col) {
std::cin >> adjMatrix[line][col];
}
}
auto exists = [adjMatrix] (int src, int dst) -> bool {
return adjMatrix[src][dst] != 0;
};
for (int through = 0; through < n; ++through) {
for (int src = 0; src < n; ++src) {
if (!exists(src, through) || src == through) {
continue;
}
for (int dst = 0; dst < n; ++dst) {
if (!exists(through, dst) || src == dst || through == dst) {
continue;
}
if (!exists(src, dst) || adjMatrix[src][dst] > adjMatrix[src][through] + adjMatrix[through][dst]) {
adjMatrix[src][dst] = adjMatrix[src][through] + adjMatrix[through][dst];
}
}
}
}
for (int line = 0; line < n; ++line) {
for (int col = 0; col < n; ++col) {
std::cout << adjMatrix[line][col] << ' ';
}
std::cout << '\n';
}
return 0;
}