Pagini recente » Cod sursa (job #1129160) | Cod sursa (job #1656769) | Cod sursa (job #2123426) | Cod sursa (job #1850432) | Cod sursa (job #3336412)
#include <fstream>
using namespace std;
ifstream fin("royfloyd.in");
ofstream fout("royfloyd.out");
const int MAX_N = 100;
int n;
int cost[MAX_N][MAX_N];
void read() {
fin >> n;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
fin >> cost[i][j];
}
}
}
void roy_floid() {
for (int k = 0; k < n; ++k) {
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (cost[i][k] && cost[k][j] && (cost[i][j] > cost[i][k] + cost[k][j] || !cost[i][j]) && i != j) {
cost[i][j] = cost[i][k] + cost[k][j];
}
}
}
}
}
void write() {
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
fout << cost[i][j] << ' ';
}
fout << '\n';
}
}
int main() {
read();
roy_floid();
write();
return 0;
}