Cod sursa(job #2425019)

Utilizator Sergiu.VictorTalmacel Sergiu Victor Sergiu.Victor Data 24 mai 2019 08:07:36
Problema Floyd-Warshall/Roy-Floyd Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.96 kb
#include <iostream>
#include <fstream>

using namespace std;

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

const int N = 101;
const int INF = 1001;
int n;
int A[N][N];

void citire() {
    fin >> n;
    int x;
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= n; j++) {
            fin >> x;
            if (i != j)
                if (x == 0) A[i][j] = INF;
                else A[i][j] = x;
            else A[i][j] = 0;
        }
}

void RoyFloyd() {
    for (int i = 1; i <= n; i++)
        for (int k = 1; k <= n; k++)
            for (int j = 1; j <= n; j++)
                if (A[i][k] + A[k][j] < A[i][j])
                    A[i][j] = A[i][k] + A[k][j];
}

void afis() {
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <=n; j++)
            if (A[i][j] == INF && i != j) fout << 0 << " ";
            else fout << A[i][j] << " ";
        fout << '\n';
    }

}

int main() {
    citire();
    RoyFloyd();
    afis();


    return 0;
}