Pagini recente » Cod sursa (job #1396237) | Cod sursa (job #27450) | Cod sursa (job #2156971) | Cod sursa (job #1609727) | Cod sursa (job #3350476)
#include <bits/stdc++.h>
#include <fstream>
using namespace std;
const int inf = 1000000000;
int mat[505][505];
int main() {
ifstream fin("royfloyd.in");
ofstream fout("royfloyd.out");
int n;
fin >> n;
for(int i = 1; i <= n; ++i) {
for(int j = 1; j <= n; ++j) {
int aux;
fin >> aux;
if(i == j) {
mat[i][j] = 0;
}
else if(aux == 0) {
mat[i][j] = inf;
}
else {
mat[i][j] = aux;
}
}
}
for(int k = 1; k <= n; ++k) {
for(int i = 1; i <= n; ++i) {
for(int j = 1; j <= n; ++j) {
if(mat[i][k] != inf && mat[k][j] != inf) {
if(mat[i][k] + mat[k][j] < mat[i][j]) {
mat[i][j] = mat[i][k] + mat[k][j];
}
}
}
}
}
for(int i = 1; i <= n; ++i) {
for(int j = 1; j <= n; ++j) {
if(mat[i][j] == inf) {
fout << 0 << " ";
}
else {
fout << mat[i][j] << " ";
}
}
fout << '\n';
}
return 0;
}