Cod sursa(job #2430323)

Utilizator bluestorm57Vasile T bluestorm57 Data 14 iunie 2019 12:10:05
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.83 kb
#include <bits/stdc++.h>

using namespace std;

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

const int NMAX = 105;
int n,mat[NMAX][NMAX];

void citire(){
    int i,j;
    f >> n;
    for(i = 1 ; i <= n ; i++)
        for(j = 1 ; j <= n ; j++)
            f >> mat[i][j];
}

void floyd(){
    int i,j,k;
    for(k = 1 ; k <= n ; k++)
        for(i = 1 ; i <= n ; i++)
            for(j = 1 ; j <= n ; j++)
                if(mat[i][k] && mat[k][j] && (mat[i][j] > mat[i][k] + mat[k][j] || !mat[i][j]) && i != j )
                    mat[i][j] = mat[i][k] + mat[k][j];
}

void afisare(){
    int i,j;
     for(i = 1 ; i <= n ; i++){
        for(j = 1 ; j <= n ; j++)
            g << mat[i][j] << " ";
        g << "\n";
    }
}

int main(){
    citire();
    floyd();
    afisare();
    return 0;
}