Pagini recente » Cod sursa (job #2928771) | Cod sursa (job #3190211) | Cod sursa (job #2882193) | Cod sursa (job #253474) | Cod sursa (job #2611504)
#include <bits/stdc++.h>
using namespace std;
#define NMAX 110
#define oo (1 << 20)
class Task {
public:
void solve() {
read_input();
get_result();
print_output();
}
private:
int n;
int dist[NMAX][NMAX];
void read_input() {
ifstream in("royfloyd.in");
in >> n;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
int cost;
in >> cost;
if (cost == 0) {
cost = oo;
}
dist[i][j] = cost;
}
}
in.close();
}
void get_result() {
Roy_Floyd();
}
void Roy_Floyd() {
for (int k = 1; k <= n; ++k) {
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
if (i != j && dist[i][k] + dist[k][j] < dist[i][j]) {
dist[i][j] = dist[i][k] + dist[k][j];
}
}
}
}
}
void print_output() {
ofstream out("royfloyd.out");
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
if (dist[i][j] == oo) {
dist[i][j] = 0;
}
out << dist[i][j] << " ";
}
}
out << "\n";
out.close();
}
};
int main() {
Task *task = new Task();
task->solve();
delete task;
return 0;
}