Cod sursa(job #2611736)

Utilizator Miruna_OrzataOrzata Miruna-Narcisa Miruna_Orzata Data 7 mai 2020 15:45:12
Problema Floyd-Warshall/Roy-Floyd Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.15 kb
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <iostream>
#include <vector>
#define NMAX 109
#define oo (1 << 20)
using namespace std;

int n;
int d[NMAX][NMAX], p[NMAX][NMAX];

void read_input() {
  cin >> n;

  for (int x = 1; x <= n; ++x) {
    for (int y = 1; y <= n; ++y) {
      int c;
      cin >> c;

      if (c == 0) {
        c = oo;
      }

      d[x][y] = c;
      p[x][y] = x;
    }
  }
}

void solve() { RoyFloyd(); }

void RoyFloyd() {
  for (int k = 1; k <= n; ++k) {
    for (int i = 1; i <= n; ++i) {
      for (int j = 1; j <= n; ++j) {
        if (i != j && d[i][k] + d[k][j] < d[i][j]) {
          d[i][j] = d[i][k] + d[k][j];
          p[i][j] = p[k][j];
        }
      }
    }
  }
}

void print_output() {
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= n; ++j) {
      if (d[i][j] == oo) {
        d[i][j] = 0;
      }
      cout << d[i][j] << " ";
    }
    cout << "\n";
  }
}

int main() {
  assert(freopen("royfloyd.in", "r", stdin) != NULL);
  assert(freopen("royfloyd.out", "w", stdout) != NULL);

  read_input();
  solve();
  print_output();

  return 0;
}