Cod sursa(job #2698242)

Utilizator codustef122Smeu Stefan codustef122 Data 21 ianuarie 2021 15:54:33
Problema Floyd-Warshall/Roy-Floyd Scor 50
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.07 kb
#include<iostream>
#include <fstream>

using namespace std;

ifstream fin("royfloyd.in");
ofstream fout("royfloyd.out");
//Cost matrix of the graph
int** costMat;
int N;

void read() {
    fin >> N;
    costMat = new int* [N];
    for (int i = 0; i < N; i++) {
        costMat[i] = new int[N];
        for (int j = 0; j < N; j++)
            fin >> costMat[i][j];

    }
        
}

void floydWarshal() {
    int** cost = new int*[N];
    // copy cost matrix
    for (int i = 0; i < N; i++) {
        cost[i] = new int[N];
        for (int j = 0; j < N; j++)
            cost[i][j] = costMat[i][j];

    }
        
    for (int k = 0; k < N; k++) {
        for (int i = 0; i < N; i++)
            for (int j = 0; j < N; j++)
                if (cost[i][k] + cost[k][j] < cost[i][j])
                    cost[i][j] = cost[i][k] + cost[k][j];
    }

    // print
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++)
            fout << cost[i][j]<<" ";
        fout << endl;
    }
}

int main() {

    read();
    floydWarshal();
}