Cod sursa(job #2979016)

Utilizator vlad21Ene Vlad - Mihnea vlad21 Data 14 februarie 2023 18:35:11
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.98 kb

#include <fstream>

using namespace std;
ifstream cin("royfloyd.in");
ofstream cout("royfloyd.out");

const int NMAX = 105, INF = 1e9;

int mat[NMAX][NMAX], c[NMAX][NMAX];

int main()
{
    int n;
    cin >> n;
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= n; j++)
            mat[i][j] = INF;
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= n ; j++){
            cin >> c[i][j];
            if(c[i][j])
                mat[i][j] = min(mat[i][j], c[i][j]);
        }
    for(int k = 1; k <= n; k++)
        for(int i = 1; i <= n; i++)
            for(int j = 1; j <= n; j++)
                if(mat[i][k] != INF &&  mat[k][j] != INF && i != j)
                    mat[i][j] = min(mat[i][k] + mat[k][j], mat[i][j]);
    for(int i = 1; i <= n; i++){
        for(int j  = 1; j <= n; j++){
            if(mat[i][j] == INF)
                mat[i][j] = 0;
            cout << mat[i][j] << " ";
        }
        cout << '\n';
    }
    return 0;
}