Cod sursa(job #1743504)

Utilizator fluture.godlikeGafton Mihnea Alexandru fluture.godlike Data 18 august 2016 11:46:57
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.17 kb
#include <cstdio>
#include <algorithm>

#define in "royfloyd.in"
#define out "royfloyd.out"
#define NMAX 100 + 7
#define inf 1000 + 7

using namespace std;
int n, mat[NMAX][NMAX];

inline void getInput()
{
    scanf("%d ", &n);
    for(int i = 1; i<= n; ++i)
    {
        for(int j = 1; j<= n; ++j)
        {
            scanf("%d ", &mat[i][j]);
            if(mat[i][j] == 0) mat[i][j] = inf;
        }
    }
}
inline void buildDynamic()
{
    for(int k = 1; k<= n; ++k)
    {
        for(int i = 1; i<= n; ++i)
        {
            if(i == k) continue;
            for(int j = 1; j<= n; ++j)
            {
                if(j == i || j == k) continue;
                mat[i][j] = min(mat[i][j], mat[i][k] + mat[k][j]);
            }
        }
    }
}
inline void giveOutput()
{
    for(int i = 1; i<= n; ++i)
    {
        for(int j = 1; j<= n; ++j)
        {
            if(mat[i][j] >= inf) mat[i][j] = 0;
            printf("%d ", mat[i][j]);
        }
        printf("\n");
    }
}

int main()
{
    freopen(in, "r", stdin);
    freopen(out, "w", stdout);
    getInput();
    buildDynamic();
    giveOutput();
    return 0;
}