Cod sursa(job #2943083)

Utilizator vlad_dimuVlad Dimulescu vlad_dimu Data 20 noiembrie 2022 16:06:21
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.81 kb
#include <fstream>
#define MAXN 100
#define INF 100001

using namespace std;

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

int cost[MAXN + 1][MAXN + 1];

int main(){
    int n, i, j, k;
    fin >> n;
    for( i = 1; i <= n; i++ ){
      for( j = 1; j <= n; j++ ){
        fin >> cost[i][j];
        if( cost[i][j] == 0 )
          cost[i][j] = INF;
      }
    }

    for( k = 1; k <= n; k++ )
      for( i = 1; i <= n; i++ )
        for( j = 1; j <= n; j++ )
          if( i != j )
            cost[i][j] = min( cost[i][j], cost[i][k] + cost[k][j] );

    for( i = 1; i <= n; i++ ){
      for( j = 1; j <= n; j++ ){
        if( cost[i][j] == INF )
          fout << "0 ";
        else
          fout << cost[i][j] << ' ';
      }
      fout << '\n';
    }

    return 0;
}