Pagini recente » Cod sursa (job #1832505) | Cod sursa (job #672132) | Cod sursa (job #2861327) | Cod sursa (job #386269) | Cod sursa (job #2856236)
#include <fstream>
#include <algorithm>
std::ifstream in("royfloyd.in");
std::ofstream out("royfloyd.out");
constexpr int N = 101;
constexpr int INF = 1e5+1;
int cost[N][N], n;
void royfloyd(){
for(int k=0; k<n; ++k) for(int i=0; i<n; ++i) for(int j=0; j<n; ++j)
cost[i][j] +=
(cost[i][k] + cost[k][j]) * (cost[i][k] + cost[k][j] < cost[i][j])
- cost[i][j] * (cost[i][k] + cost[k][j] < cost[i][j]);
}
int main(){
in >> n;
for(int i=0; i<n; ++i) for(int j=0; j<n; ++j){
in >> cost[i][j];
cost[i][j] += INF * (i!=j && !cost[i][j]) - cost[i][j] * (i!=j && !cost[i][j]);
}
royfloyd();
for(int i=0; i<n; ++i){
for(int j=0; j<n; ++j)
out << (cost[i][j] != INF ? cost[i][j] : 0) << ' ';
out << '\n';
}
}