Cod sursa(job #2569991)

Utilizator AlexPop28Pop Alex-Nicolae AlexPop28 Data 4 martie 2020 14:34:48
Problema Floyd-Warshall/Roy-Floyd Scor 50
Compilator cpp-64 Status done
Runda r3capitusulare Marime 0.72 kb
#include <bits/stdc++.h>
#define dbg() cerr <<
#define name(x) (#x) << ": " << (x) << ' ' <<

using namespace std;

const int NMAX = 100;

int dist[NMAX][NMAX];

int main() {
  ifstream cin("royfloyd.in");
  ofstream cout("royfloyd.out");

  int n; cin >> n;
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < n; ++j)
      cin >> dist[i][j];

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

  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j)
      cout << dist[i][j] << ' ';
    cout << '\n';
  }
}