Cod sursa(job #2524383)

Utilizator greenadexIulia Harasim greenadex Data 15 ianuarie 2020 17:00:20
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.18 kb
#include <fstream>

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

const int kMaxNumNodes = 101;

int shortestPath[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];
    }
  }

  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] == 0 ||
            shortestPath[k][j] == 0) {
          continue;
        }

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

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

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

  return 0;
}