Cod sursa(job #2425832)

Utilizator gabrielmGabriel Majeri gabrielm Data 25 mai 2019 07:53:15
Problema Floyd-Warshall/Roy-Floyd Scor 50
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.83 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 cost_nou = mat[i][k] + mat[k][j];
                if (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';
    }
}