Cod sursa(job #2856236)

Utilizator Albert_GAlbert G Albert_G Data 23 februarie 2022 16:36:23
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.82 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)
        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';
    }
}