Cod sursa(job #2425834)

Utilizator gabrielmGabriel Majeri gabrielm Data 25 mai 2019 07:59:35
Problema Floyd-Warshall/Roy-Floyd Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.97 kb
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

int main() {
    ifstream in("royfloyd.in");

    int n;
    in >> n;

    vector<vector<int>> mat(n, vector<int>(n));

    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            in >> mat[i][j];
        }
    }


    for (int k = 0; k < n; ++k) {
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < n; ++j) {
                int& cost_vechi = mat[i][j];

                int a = mat[i][k], b = mat[k][j];
                if (a == 0 || b == 0) {
                    continue;
                }

                int cost_nou = a + b;
                if (cost_vechi == 0 || cost_nou < cost_vechi) {
                    cost_vechi = cost_nou;
                }
            }
        }
    }

    ofstream out("royfloyd.out");

    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            out << mat[i][j] << ' ';
        }
        out << '\n';
    }
}