Pagini recente » Cod sursa (job #708446) | Cod sursa (job #1275439) | Cod sursa (job #1990890) | Cod sursa (job #1559888) | Cod sursa (job #2469459)
#include <fstream>
#include <iostream>
using namespace std;
#define NMAX 100
#define MAXDIST 1001
ifstream in("royfloyd.in");
ofstream out("royfloyd.out");
int a[NMAX][NMAX], n, dist[NMAX][NMAX];
void getArray();
void floydWarshall();
int main(){
in >> n;
getArray();
floydWarshall();
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
out<<dist[i][j]<<" ";
}
out<<"\n";
}
return 0;
}
void getArray(){
for (int i = 0; i < n; i++){
for (int j = 0; j < n; j++){
in>>a[i][j];
dist[i][j] = a[i][j];
}
}
}
void floydWarshall(){
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
if(dist[i][j] == 0 && i != j){
dist[i][j] = MAXDIST;
}
}
}
for(int k = 0; k < n; k++){
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
int temp = dist[i][k] + dist[k][j];
if(dist[i][j] > temp){
dist[i][j] = temp;
}
}
}
}
}