Pagini recente » Cod sursa (job #3121304) | Cod sursa (job #44710) | Cod sursa (job #2333323) | Cod sursa (job #759887) | Cod sursa (job #2698242)
#include<iostream>
#include <fstream>
using namespace std;
ifstream fin("royfloyd.in");
ofstream fout("royfloyd.out");
//Cost matrix of the graph
int** costMat;
int N;
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];
}
}
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++)
fout << cost[i][j]<<" ";
fout << endl;
}
}
int main() {
read();
floydWarshal();
}