Cod sursa(job #2859098)

Utilizator Albert_GAlbert G Albert_G Data 28 februarie 2022 20:49:14
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.69 kb
#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)
        if(cost[i][k] + cost[k][j] < cost[i][j]) cost[i][j] = cost[i][k] + cost[k][j];
}
	
 
	
int main(){
    in >> n;
	
    for(int i=0; i<n; ++i) for(int j=0; j<n; ++j){
        in >> cost[i][j];
        if(i!=j && !cost[i][j]) cost[i][j] = INF;
    }
	
    royfloyd();
	
    for(int i=0; i<n; ++i){
        for(int j=0; j<n; ++j)
            out << cost[i][j] << ' ';
        out << '\n';
    }
	
    return 0;
}