Cod sursa(job #2334647)

Utilizator IOI_MDA_003Sebastian Chicu IOI_MDA_003 Data 2 februarie 2019 20:04:34
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.91 kb
#include <iostream>
#include <fstream>

using namespace std;

ifstream fin ("royfloyd.in");
ofstream fout("royfloyd.out");

const int N_MAX = 100 + 5;
const int INF = 0x3f3f3f3f;

int x[N_MAX][N_MAX];

int n;

int main(){
    
    fin >> n;
    for(int i = 1; i <=n; ++i)
        for(int j = 1, nr; j <=n; ++j){
            fin >> nr; 
            if(nr == 0)
                x[i][j] = INF;
            else 
                x[i][j] = nr;
        }

    for(int k = 1; k <=n; ++k)
        for(int i = 1; i<=n; ++i)
            for(int j = 1; j <=n; ++j)
                if(i != j and x[i][j] > x[i][k] + x[k][j])
                    x[i][j] = x[i][k] + x[k][j]; 
    
    for(int i = 1; i <=n; ++i, fout << "\n")
        for(int j = 1, nr; j <=n; ++j, fout << " ")
            if(x[i][j] >= INF)
                fout << "0";
            else 
                fout << x[i][j];

    return 0;
}
// român convertit la moldovenism