Cod sursa(job #2395984)

Utilizator alexc2k00Ciornei Alexandru alexc2k00 Data 3 aprilie 2019 09:19:25
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.72 kb
#include <bits/stdc++.h>
using namespace std;

ifstream f("royfloyd.in");
ofstream g("royfloyd.out");
int a[1001][1001];
int n;

void citire() {
    f >> n;

    for(int i=1; i<=n; i++)
        for(int j=1; j<=n; j++)
            f >> a[i][j];
}

void roy() {
    for(int k=1; k<=n; k++)
        for(int i=1; i<=n; i++)
            for(int j=1; j<=n; j++)
                if(a[i][k] and a[k][j] and (a[i][j] > a[i][k] + a[k][j] or a[i][j] == 0) and i != j)
                    a[i][j] = a[i][k] + a[k][j];
}

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

int main() {
    citire();
    roy();
    afisare();
}