Cod sursa(job #2698249)

Utilizator codustef122Smeu Stefan codustef122 Data 21 ianuarie 2021 16:08:03
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.27 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;
int const inf = 1001;

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];
            if (costMat[i][j] == 0 && i!=j)
                costMat[i][j] = inf;
        }
            
    }
        
}

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++) {
            if (cost[i][j] == inf) fout << 0 << " ";
            else  fout << cost[i][j] << " ";
        }
        fout << endl;
    }
}

int main() {

    read();
    floydWarshal();
}