Cod sursa(job #2457457)

Utilizator gabriel_212MitracheG gabriel_212 Data 17 septembrie 2019 20:08:56
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.88 kb
#include <iostream>
#include <fstream>

using namespace std;

int matrice[101][101]{},n;

ifstream fin("royfloyd.in");
ofstream fout("royfloyd.out");

void citire(){


   fin>>n;

   for(int i=1;i<=n;i++){

    for(int j=1;j<=n;j++){

        fin>>matrice[i][j];

        if(i==j){

            matrice[i][j]=0;

        }

    }

   }

}
void roy_floyd(){

int i,j,k;

for( k=1;k<=n;k++){

    for( i=1;i<=n;i++){

       for( j=1;j<=n;j++){

        if((matrice[i][j]>matrice[i][k]+matrice[k][j]||!matrice[i][j])&&i!=j&&matrice[i][k]&&matrice[k][j]){

            matrice[i][j]=matrice[i][k]+matrice[k][j];
         }

}
}
}
}
void afisare(){

for(int i=1;i<=n;i++){
    for(int j=1;j<=n;j++){
    fout<<matrice[i][j]<<" ";
}
fout<<"\n";
}
}

int main()
{citire();
roy_floyd();
afisare();
fin.close();
fout.close();

    return 0;
}