Cod sursa(job #2860976)

Utilizator vladburacBurac Vlad vladburac Data 3 martie 2022 11:44:28
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.82 kb
#include <iostream>
#include <fstream>
using namespace std;
const int MAXN = 100;

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

int a[MAXN+1][MAXN+1];
int dist[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 >> a[i][j];
  }
  for( i = 1; i <= n; i++ ) {
    for( j = 1; j <= n; j++ ) {
      if( a[i][j] || i == j )
        dist[i][j] = a[i][j];
      else
        dist[i][j] = 1e7;
    }
  }
  for( k = 1; k <= n; k++ ) {
    for( i = 1; i <= n; i++ ) {
      for( j = 1; j <= n; j++ ) {
        dist[i][j] = min( dist[i][j], dist[i][k] + dist[k][j] );
      }
    }
  }
  for( i = 1; i <= n; i++ ) {
    for( j = 1; j <= n; j++ )
      fout << dist[i][j] << " ";
    fout << "\n";
  }
  return 0;
}