Pagini recente » Cod sursa (job #321291) | Cod sursa (job #442550) | Cod sursa (job #506518) | Cod sursa (job #1038002) | Cod sursa (job #2198467)
#include <iostream>
#include <fstream>
using namespace std;
int n, m;
int** M;
void read() {
fstream fin("royfloyd.in", ios::in);
fin >> n;
M = new int*[n];
for (int i = 0; i < n; i++) {
M[i] = new int[n];
for (int j = 0; j < n; j++) {
fin >> M[i][j];
if ((M[i][j] == 0) && (i != j)) {
M[i][j] = 1 << 29;
}
}
}
fin.close();
}
void roy_floyd() {
for (int k = 0; k < n; k++) {
for (int x = 0; x < n; x++) {
for (int y = 0; y < n; y++) {
if (M[x][y] > M[x][k] + M[k][y] && x != y) {
M[x][y] = M[x][k] + M[k][y];
}
}
}
}
}
void write() {
fstream fout("royfloyd.out", ios::out);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (M[i][j] == 1 << 29) {
fout << "0 ";
} else {
fout << M[i][j] << " ";
}
}
fout << "\n";
}
fout.close();
}
int main() {
read();
roy_floyd();
write();
return 0;
}