Cod sursa(job #3296229)

Utilizator Traian_7109Traian Mihai Danciu Traian_7109 Data 12 mai 2025 10:26:35
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.84 kb
#include <fstream>
#include <vector>

using namespace std;

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

const int MAXN = 100;
const int INFINIT = 1000000001;

int dist[MAXN + 1][MAXN + 1];

int main() {
  int n;
  fin >> n;
  for(int i = 1; i <= n; i++) {
    for(int j = 1; j <= n; j++) {
      fin >> dist[i][j];
      if(dist[i][j] == 0) {
        dist[i][j] = INFINIT;
      }
    }
  }

  for(int k = 1; k <= n; k++) {
    for(int i = 1; i <= n; i++) {
      for(int j = 1; j <= n; j++) {
        dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
      }
    }
  }

  for(int i = 1; i <= n; i++) {
    for(int j = 1; j <= n; j++) {
      if(dist[i][j] == INFINIT || i == j) {
        dist[i][j] = 0;
      }
      fout << dist[i][j] << " ";
    }
    fout << "\n";
  }

  return 0;
}