Pagini recente » Cod sursa (job #842418) | Cod sursa (job #2433045) | Cod sursa (job #2674265) | Cod sursa (job #1409114) | Cod sursa (job #2611340)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#include <string>
#define NMAX 109
#define oo (1 << 20)
using namespace std;
class Task {
public:
void solve() {
read_input();
get_result();
print_output();
}
private:
int n;
int d[NMAX][NMAX], p[NMAX][NMAX];
void read_input() {
ifstream fin ("royfloyd.in");
fin >> n;
for (int x = 1; x <= n; ++x) {
for (int y = 1; y <= n; ++y) {
int c;
fin >> c;
if (c == 0) {
c = oo;
}
d[x][y] = c;
p[x][y] = x;
}
}
}
void get_result() {
RoyFloyd();
}
void RoyFloyd() {
for (int k = 1; k <= n; ++k) {
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
if (i != j && d[i][k] + d[k][j] < d[i][j]) {
d[i][j] = d[i][k] + d[k][j];
p[i][j] = p[k][j];
}
}
}
}
}
void print_output() {
ofstream fout("royfloyd.out");
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
if (d[i][j] == oo) {
d[i][j] = 0;
}
fout << d[i][j] << " ";
}
fout << "\n";
}
fout.close();
}
};
int main() {
Task *task = new Task();
task->solve();
delete task;
return 0;
}