Cod sursa(job #2943077)

Utilizator cyg_mihaizMIHAI ZARAFIU cyg_mihaiz Data 20 noiembrie 2022 16:00:10
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.01 kb
#include <fstream>

using std::ios_base;

const int INF = 1e9;
const short NMAX = 100;

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

int mat[1 + NMAX][1 + NMAX];

int main()
{
    ios_base::sync_with_stdio(false);
    fin.tie(NULL);

    int n,i,j,k,x;

    fin >> n;
    for(i = 1; i <= n; i++)
        for(j = 1; j <= n; j++)
        {
            fin >> x;
            if(i == j)
                mat[i][j] = 0;
            else
                if(x)
                    mat[i][j] = x;
                else
                    mat[i][j] = INF;
        }

    for(k = 1; k <= n; k++)
        for(i = 1; i <= n; i++)
            for(j = 1; j <= n; j++)
                mat[i][j] = std::min(mat[i][j], mat[i][k] + mat[k][j]);

    for(i = 1; i <= n; i++)
    {
        for(j = 1; j <= n; j++)
            if(mat[i][j] == INF)
                fout << "0 ";
            else
                fout << mat[i][j] << " ";
        fout << "\n";
    }

    return 0;
}