Pagini recente » Cod sursa (job #2344768) | Cod sursa (job #1637119) | Cod sursa (job #2154609) | Cod sursa (job #1464261) | Cod sursa (job #2698247)
#include<iostream>
#include <fstream>
#include <iomanip>
using namespace std;
ifstream fin("royfloyd.in");
ofstream fout("royfloyd.out");
//Cost matrix of the graph
int** costMat;
int N;
int const inf = (1 << 30);
void read() {
fin >> N;
costMat = new int* [N];
for (int i = 0; i < N; i++) {
costMat[i] = new int[N];
for (int j = 0; j < N; j++) {
fin >> costMat[i][j];
if (costMat[i][j] == 0 && i!=j)
costMat[i][j] = inf;
}
}
}
void floydWarshal() {
int** cost = new int*[N];
// copy cost matrix
for (int i = 0; i < N; i++) {
cost[i] = new int[N];
for (int j = 0; j < N; j++)
cost[i][j] = costMat[i][j];
}
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][j] = cost[i][k] + cost[k][j];
}
// print
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (cost[i][j] == inf || cost[i][j]<0) fout << 0 << " ";
else fout << cost[i][j] << " ";
}
fout << endl;
}
}
int main() {
read();
floydWarshal();
}