Cod sursa(job #3239843)

Utilizator ChopinFLazar Alexandru ChopinF Data 7 august 2024 23:55:22
Problema Floyd-Warshall/Roy-Floyd Scor 50
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.12 kb
#include <bits/stdc++.h>
using namespace std;
std::string file = "royfloyd";
std::ifstream fin(file + ".in");
std::ofstream fout(file + ".out");
// #define fin std::cin
// #define fout std::cout
int n;
std::vector<std::vector<int>> gf, costuri;

int32_t main(int32_t argc, char *argv[]) {
  ios_base::sync_with_stdio(false);
  fin.tie(0);
  fout.tie(0);
  fin >> n;

  gf.resize(n + 1, std::vector<int>(n + 1, INT_MAX));
  costuri.resize(n + 1, std::vector<int>(n + 1, INT_MAX));

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

  for (int k = 1; k <= n; ++k) {
    for (int i = 1; i <= n; ++i) {
      for (int j = 1; j <= n; ++j) {
        if (costuri[i][k] != INT_MAX && costuri[k][j] != INT_MAX) {
          costuri[i][j] =
              std::min(costuri[i][j], costuri[i][k] + costuri[k][j]);
        }
      }
    }
  }

  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= n; ++j) {
      fout << (costuri[i][j] == INT_MAX ? -1 : costuri[i][j]) << " ";
    }
    fout << "\n";
  }

  return 0;
}