Cod sursa(job #2899586)

Utilizator preda.andreiPreda Andrei preda.andrei Data 8 mai 2022 22:42:33
Problema Distante Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.37 kb
#include <fstream>
#include <vector>

using namespace std;

using Graph = vector<vector<pair<int, int>>>;

bool Solve(Graph& graph, int source, const vector<int>& dists) {
    if (dists[source] != 0) {
        return false;
    }

    vector<bool> has_exact(graph.size(), false);
    for (size_t i = 0; i < graph.size(); i += 1) {
        for (const auto& [j, cost] : graph[i]) {
            if (dists[i] + cost < dists[j]) {
                return false;
            }
            has_exact[j] = has_exact[j] || dists[i] + cost == dists[j];
        }
    }
    for (int i = 0; i < (int)graph.size(); i += 1) {
        if (i != source && !has_exact[i]) {
            return false;
        }
    }
    return true;
}

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

    int tests;
    fin >> tests;

    for (int i = 0; i < tests; i += 1) {
        int nodes, edges, source;
        fin >> nodes >> edges >> source;

        vector<int> dists(nodes);
        for (auto& dist : dists) {
            fin >> dist;
        }

        Graph graph(nodes);
        for (int j = 0; j < edges; j += 1) {
            int a, b, cost;
            fin >> a >> b >> cost;
            graph[a - 1].push_back({b - 1, cost});
            graph[b - 1].push_back({a - 1, cost});
        }
        fout << (Solve(graph, source - 1, dists) ? "DA" : "NU") << "\n";
    }
    return 0;
}