Pagini recente » Cod sursa (job #1293396) | Cod sursa (job #2426945) | Profil M@2Te4i | Rating Laurentiu Dumitrescu (Laurentiu28) | Cod sursa (job #2247012)
#include <fstream>
#include <vector>
using namespace std;
int main()
{
ifstream fin("royfloyd.in");
ofstream fout("royfloyd.out");
int nodes;
fin >> nodes;
vector<vector<int>> costs(nodes, vector<int>(nodes));
for (auto &row : costs) {
for (auto &elem : row) {
fin >> elem;
}
}
for (int start = 0; start < nodes; start += 1) {
for (int end = 0; end < nodes; end += 1) {
if (start == end) {
continue;
}
for (int inter = 0; inter < nodes; inter += 1) {
if (costs[start][inter] != 0 && costs[inter][end] != 0) {
auto new_cost = costs[start][inter] + costs[inter][end];
if (costs[start][end] == 0) {
costs[start][end] = new_cost;
} else {
costs[start][end] = min(costs[start][end], new_cost);
}
}
}
}
}
for (const auto &row : costs) {
for (const auto &elem : row) {
fout << elem << " ";
}
fout << "\n";
}
return 0;
}