Cod sursa(job #2703258)

Utilizator ProBatmanBalint Leonard ProBatman Data 7 februarie 2021 20:53:29
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.49 kb
#include <bits/stdc++.h>

using namespace std;

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

const int CMAX = 1e2+15 , INF = 10015;
int mat[CMAX][CMAX] , newmat[CMAX][CMAX] , n;

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

void royfloyd()
{
    for(int cont=1;cont<=n;cont++)
    {
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
            {
                if(i!=cont&&j!=cont)
                {
                    if(mat[i][j]>mat[i][cont]+mat[cont][j]){
                        /*
                        cout << "schimbare : " << i << " " << cont << "||" << cont << " " << j << '\n';
                        cout << "coordonate : " << i << " " << j << '\n';
                        cout << "Valoare : " << mat[i][j] << '\n';
                        cout << "noua valoare : " << mat[i][cont] + mat[cont][j] << '\n' << '\n';
                        */
                        mat[i][j] = mat[i][cont] + mat[cont][j];
                    }
                }
            }
        }
    }
}

void show()
{
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
            fout << mat[i][j] << " ";
        fout << '\n';
    }
}

int main()
{
    read();
    royfloyd();
    show();
    return 0;
}