Cod sursa(job #2700898)

Utilizator cyg_mihaizMIHAI ZARAFIU cyg_mihaiz Data 29 ianuarie 2021 11:45:11
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.01 kb
#include <fstream>

using namespace std;
const short NMAX = 100;
const int INF = 1000000000;

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

int mat[NMAX + 5][NMAX + 5];

int main()
{
    ios_base::sync_with_stdio(false);
    fin.tie(NULL);
    fout.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] = 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;
}