Pagini recente » Cod sursa (job #2192357) | Cod sursa (job #911253) | Cod sursa (job #2572228) | Cod sursa (job #501860) | Cod sursa (job #2856228)
#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] << ' ';
out << '\n';
}
}