Cod sursa(job #3142065)

Utilizator RolandPetreanPetrean Roland RolandPetrean Data 18 iulie 2023 18:18:17
Problema Floyd-Warshall/Roy-Floyd Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.68 kb
// https://infoarena.ro/problema/royfloyd
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'

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

const int INF = 1e9;

int main() {
  int n;
  fin>>n;

  vector<vector<int>> dist(n+1, vector<int>(n+1));
  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] = INF;
    }
  }

  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) fout<<(dist[i][j] == INF ? 0 : dist[i][j])<<" ";
    fout<<endl;
  }
}