Cod sursa(job #2247012)

Utilizator preda.andreiPreda Andrei preda.andrei Data 27 septembrie 2018 19:32:49
Problema Floyd-Warshall/Roy-Floyd Scor 20
Compilator cpp Status done
Runda Arhiva educationala Marime 1.15 kb
#include <fstream>
#include <vector>

using namespace std;

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

    int nodes;
    fin >> nodes;

    vector<vector<int>> costs(nodes, vector<int>(nodes));
    for (auto &row : costs) {
        for (auto &elem : row) {
            fin >> elem;
        }
    }

    for (int start = 0; start < nodes; start += 1) {
        for (int end = 0; end < nodes; end += 1) {
            if (start == end) {
                continue;
            }

            for (int inter = 0; inter < nodes; inter += 1) {
                if (costs[start][inter] != 0 && costs[inter][end] != 0) {
                    auto new_cost = costs[start][inter] + costs[inter][end];
                    if (costs[start][end] == 0) {
                        costs[start][end] = new_cost;
                    } else {
                        costs[start][end] = min(costs[start][end], new_cost);
                    }
                }
            }
        }
    }

    for (const auto &row : costs) {
        for (const auto &elem : row) {
            fout << elem << " ";
        }
        fout << "\n";
    }

    return 0;
}