Pagini recente » Cod sursa (job #2814969) | Cod sursa (job #58539) | Cod sursa (job #960741) | Cod sursa (job #2078367) | Cod sursa (job #3333967)
#include <fstream>
#include <vector>
#include <iostream>
#define INF 0x3f3f3f3f
std::ifstream fin("royfloyd.in");
std::ofstream fout("royfloyd.out");
void royFloyd(std::vector<std::vector<int>>&cost, int n) {
for(int k=1;k<=n;++k) {
for(int i=1;i<=n;++i) {
for(int j=1;j<=n;++j) {
if(cost[i][k] + cost[k][j] < cost[i][j]) {
cost[i][j] = cost[i][k] + cost[k][j];
}
}
}
}
for(int i=1;i<=n;++i) {
for(int j=1;j<=n;++j) {
int x = (cost[i][j] == INF) ? 0 : (cost[i][j]);
fout << x << " ";
}
fout << std::endl;
}
}
int main(void) {
int n, x;
fin >> n;
std::vector<std::vector<int>>cost(n+1, std::vector<int>(n+1, INF));
for(int i = 1; i <= n; ++i) cost[i][i] = 0;
for(int i=1;i<=n;++i) {
for(int j=1;j<=n;++j) {
fin >> x;
if (x > 0) cost[i][j] = x;
}
}
royFloyd(cost, n);
return 0;
}