Cod sursa(job #2771397)

Utilizator ansalecAlecu Stefan-Iulian ansalec Data 27 august 2021 03:20:03
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.61 kb
#include <array>
#include <fstream>
#include <iostream>
using uint = unsigned int;

int main() {
  std::ifstream inFile;
  inFile.open("royfloyd.in", std::ios::in);

  if (inFile.is_open()) {
    uint nrNoduri;
    inFile >> nrNoduri;

    std::ofstream outFile;
    outFile.open("royfloyd.out", std::ios::out);
    if (outFile.is_open()) {
      std::array<uint, 10000> graf, grafSol;
      const unsigned int INFINIT = 1001;

      for (uint r = 0; r < nrNoduri; r++) {
        for (uint c = 0; c < nrNoduri; c++) {
          inFile >> graf[r * nrNoduri + c];
          if (graf[r * nrNoduri + c] == 0)
            grafSol[r * nrNoduri + c] = INFINIT;
          else
            grafSol[r * nrNoduri + c] = graf[r * nrNoduri + c];
        }
      }

      for (uint k = 0; k < nrNoduri; k++) {
        for (uint i = 0; i < nrNoduri; i++) {
          for (uint j = 0; j < nrNoduri; j++) {
            if (grafSol[i * nrNoduri + k] < INFINIT &&
                grafSol[k * nrNoduri + j] < INFINIT) {
              grafSol[i * nrNoduri + j] = std::min(
                  grafSol[i * nrNoduri + j],
                  grafSol[i * nrNoduri + k] + grafSol[k * nrNoduri + j]);
            }
          }
        }
        grafSol[k * (nrNoduri + 1)] = 0;
      }

      for (int i = 0; i < nrNoduri; i++) {
        for (int j = 0; j < nrNoduri; j++) {
          outFile << grafSol[i * nrNoduri + j] << " ";
        }
        outFile << '\n';
      }
      outFile.close();

    } else
      std::cout << "Nu s-a putut scrie in fisier!" << std::endl;
    inFile.close();
  } else
    std::cout << "Nu s-a putut deschide fisierul!" << std::endl;
  return 0;
}