Cod sursa(job #2831719)

Utilizator Teodor_AxinteAxinte Teodor-Ionut Teodor_Axinte Data 11 ianuarie 2022 22:31:46
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.53 kb
#include <fstream>

using namespace std;

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

const long long int oo = (1 << 16) - 1;
long long int w[110][110], n;

long long int floydwarshall(long long int i, long long int j, long long int k)
{
    if (k < 0) // going through no nodes
        return w[i][j];
    else
        return min(floydwarshall(i, j, k - 1),
                   floydwarshall(i, k, k - 1) + floydwarshall(k, j, k - 1)); // adding the k-th node between i and j
}
void FloydWarshall()
{
    for (int k = 0; k < n; k++)
        for (int i = 0; i < n; i++)
            for (int j = 0; j < n; j++)
                if (i == j || j == k)
                    continue;
                else
                    w[i][j] = min(w[i][j], w[i][k] +
                                               w[k][j]); // the route between i and j is the minimum of the root itself and the added roots of the new node added
}
int main()
{
    fin >> n;
    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
        {
            fin >> w[i][j];
            if (!w[i][j])
                w[i][j] = oo;
        }
    /*for(int i=0;i<n;i++)
        for(int j=0;j<n;j++)
            for(int k=0;k<n;k++)
                w[i][j]= floydwarshall(i,j,k);
    floydwarshall(0,0,0);
     */
    FloydWarshall();

    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
            if (w[i][j] == oo)
                fout << 0 << " ";
            else
                fout << w[i][j] << " ";
        fout << "\n";
    }
    return 0;
}