Cod sursa(job #2524381)

Utilizator greenadexIulia Harasim greenadex Data 15 ianuarie 2020 16:53:59
Problema Floyd-Warshall/Roy-Floyd Scor 90
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.33 kb
#include <fstream>

const std::string kInputFile = "royfloyd.in";
const std::string kOutputFile = "royfloyd.out";

const int kMaxNumNodes = 100;

int shortestPath[kMaxNumNodes][kMaxNumNodes][kMaxNumNodes];

int main() {
  std::ifstream fin(kInputFile);
  std::ofstream fout(kOutputFile);

  int num_nodes;
  fin >> num_nodes;

  for (int i = 1; i <= num_nodes; i++) {
    for (int j = 1; j <= num_nodes; j++) {
      fin >> shortestPath[i][j][0];
    }
  }

  for (int k = 1; k <= num_nodes; k++) {
    for (int i = 1; i <= num_nodes; i++) {
      for (int j = 1; j <= num_nodes; j++) {
        if (i == j) {
          continue;
        }

        if (shortestPath[i][k][k - 1] == 0 ||
            shortestPath[k][j][k - 1] == 0) {
          shortestPath[i][j][k] = shortestPath[i][j][k - 1];
          continue;
        }

        if (shortestPath[i][j][k - 1] == 0) {
          shortestPath[i][j][k] = shortestPath[i][k][k - 1] +
                                  shortestPath[k][j][k - 1];
          continue;
        }

        shortestPath[i][j][k] = std::min(shortestPath[i][j][k - 1],
            shortestPath[i][k][k - 1] + shortestPath[k][j][k - 1]);
      }
    }
  }

  for (int i = 1; i <= num_nodes; i++) {
    for (int j = 1; j <= num_nodes; j++) {
      fout << shortestPath[i][j][num_nodes] << ' ';
    }
    fout << '\n';
  }

  return 0;
}