Cod sursa(job #2198349)

Utilizator georgeromanGeorge Roman georgeroman Data 24 aprilie 2018 12:02:54
Problema Floyd-Warshall/Roy-Floyd Scor 50
Compilator cpp Status done
Runda Arhiva educationala Marime 0.88 kb
#include <algorithm>
#include <fstream>
#include <vector>

int main() {
  std::ifstream in("royfloyd.in");
  std::ofstream out("royfloyd.out");

  unsigned n;
  in >> n;
  std::vector<std::vector<int>> costMat = std::vector<std::vector<int>>();
  costMat.reserve(n);
  for (unsigned i = 0; i < n; i++) {
    costMat.push_back(std::vector<int>());
    costMat[i].reserve(n);
  }
  for (unsigned i = 0; i < n; i++) {
    for (unsigned j = 0; j < n; j++) {
      int cost;
      in >> cost;
      costMat[i].push_back(cost);
    }
  }

  for (unsigned k = 0; k < n; k++) {
    for (unsigned j = 0; j < n; j++) {
      for (unsigned i = 0; i < n; i++) {
        costMat[i][j] = std::min(costMat[i][j], costMat[i][k] + costMat[k][j]);
      }
    }
  }

  for (unsigned i = 0; i < n; i++) {
    for (unsigned j = 0; j < n; j++) {
      out << costMat[i][j] << " ";
    }
    out << "\n";
  }
  return 0;
}