Cod sursa(job #2763460)

Utilizator Teodor_AxinteAxinte Teodor-Ionut Teodor_Axinte Data 14 iulie 2021 11:44:32
Problema Floyd-Warshall/Roy-Floyd Scor 50
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.12 kb
#include <fstream>

using namespace std;

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

int w[110][110], n;

int floydwarshall(int i, int j, 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++)
                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];
    /*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++)
            fout << w[i][j] << " ";
        fout << "\n";
    }

    return 0;

}