Pagini recente » Cod sursa (job #1874262) | Cod sursa (job #1510930) | Cod sursa (job #2603232) | Cod sursa (job #1702264) | Cod sursa (job #2590144)
#include <iostream>
#include <fstream>
using namespace std;
/* Roy-Floyd: O ( n^3 ) */
#define MAX 105
#define INFINIT 10000000
ifstream fin("royfloyd.in");
ofstream fout("royfloyd.out");
int nrNoduri, matriceCosturi[MAX][MAX];
void royFloyd(){
for ( int k = 1; k <= nrNoduri; k++)
for ( int i = 1; i <= nrNoduri; i++)
for ( int j = 1; j <= nrNoduri; j++)
if ( matriceCosturi[i][j] > matriceCosturi[i][k] + matriceCosturi[k][j] )
matriceCosturi[i][j] = matriceCosturi[i][k] + matriceCosturi[k][j];
}
int main(){
fin >> nrNoduri;
for ( int i = 1; i <= nrNoduri; i++)
for ( int j = 1; j <= nrNoduri; j++)
fin >> matriceCosturi[i][j];
for ( int i = 1; i <= nrNoduri; i++)
for ( int j = 1; j <= nrNoduri; j++)
if ( i != j && matriceCosturi[i][j] == 0 )
matriceCosturi[i][j] = INFINIT;
royFloyd();
for ( int i = 1; i <= nrNoduri; i++){
for ( int j = 1; j <= nrNoduri; j++)
fout << matriceCosturi[i][j] << " ";
fout << "\n";
}
return 0;
}