Cod sursa(job #2978985)

Utilizator Laza_AndreiLazarescu Andrei Vlad Laza_Andrei Data 14 februarie 2023 18:21:40
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.89 kb
#include <cstdio>
#include <algorithm>
#define intmax 1e9/2
#define NMAX 103

using namespace std;

int c[NMAX][NMAX];

int main()
{
    freopen("royfloyd.in", "r", stdin);
    freopen("royfloyd.out", "w", stdout);

    int n, i, j, k;
    scanf("%d", &n);
    for(i = 1; i <= n; ++i)
        for(j = 1; j <= n; ++j)
        {
            scanf("%d", &c[i][j]);
            if(i != j && !c[i][j])
                c[i][j] = intmax;
        }

    for(k = 1; k <= n; ++k)
        for(i = 1; i <= n; ++i)
            for(j = 1; j <= n; ++j)
                if(i != j)
                    c[i][j] = min(c[i][j], c[i][k] + c[k][j]);
    for(i = 1; i <= n; ++i)
    {
        for(j = 1; j <= n; ++j)
            if(i != j && c[i][j] == intmax)
                printf("0 ");
            else
                printf("%d ", c[i][j]);
        printf("\n");
    }
    return 0;
}