Pagini recente » Cod sursa (job #281279) | Cod sursa (job #2014678) | Borderou de evaluare (job #2051206) | Cod sursa (job #694296) | Cod sursa (job #1799175)
#include <fstream>
#include <algorithm>
#include <vector>
using namespace std;
const int INF = 0x3f3f3f3f;
void royFloyd(vector<vector<int>>& cost) {
const int n = int(cost.size());
for (int k = 0; k < n; ++k) {
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
cost[i][j] = min(cost[i][j], cost[i][k] + cost[k][j]);
}
}
}
}
int main() {
ifstream in("royfloyd.in");
ofstream out("royfloyd.out");
int n;
in >> n;
auto cost = vector<vector<int>>(n, vector<int>(n, INF));
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
in >> cost[i][j];
if (i != j && cost[i][j] == 0) {
cost[i][j] = INF;
}
}
}
royFloyd(cost);
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
out << (cost[i][j] == INF ? 0 : cost[i][j]) << (j < n - 1 ? " " : "\n");
}
}
in.close();
out.close();
return 0;
}