Cod sursa(job #2538362)

Utilizator marius004scarlat marius marius004 Data 4 februarie 2020 18:04:36
Problema Floyd-Warshall/Roy-Floyd Scor 90
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.96 kb
#include <fstream>

std::ifstream f("royfloyd.in");
std::ofstream g("royfloyd.out");

const int NMAX = 105;
const int INF = (1LL << 31) - 1;

int n,a[NMAX][NMAX],dist[NMAX][NMAX];

void roy_floyd(){
    
    for(int k = 1;k <= n;++k)
        for(int i = 1;i <= n;++i)
            for(int j = 1;j <= n;++j)
                if(dist[i][j] > dist[i][k] + dist[k][j])
                    dist[i][j] = dist[i][k] + dist[k][j];
}

int main(){
    
    f >> n;
    
    for(int i = 1;i <= n;++i){
        for(int j = 1;j <= n;++j){
            f >> a[i][j];
            if(i == j)
                dist[i][j] = 0;
            else if(a[i][j] == 0)
                dist[i][j] = INF;
            else
                dist[i][j] = a[i][j];
        }
    }
    
    roy_floyd();
    
    for(int i = 1;i <= n;++i,g << '\n')
        for(int j = 1;j <= n;++j)
            if(dist[i][j] == INF)
                g << 0 << ' ';
            else
                g << dist[i][j] << ' ';
    
    return 0;
}