Pagini recente » Cod sursa (job #294636) | Cod sursa (job #1148346) | Cod sursa (job #79226) | Cod sursa (job #1066166) | Cod sursa (job #2198354)
#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 i = 0; i < n; i++) {
for (unsigned j = 0; j < n; j++) {
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;
}