Cod sursa(job #3125237)

Utilizator preda.andreiPreda Andrei preda.andrei Data 2 mai 2023 13:59:37
Problema Distante Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.63 kb
#include <cstdint>
#include <fstream>
#include <queue>
#include <vector>

using namespace std;

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

template <typename T>
using MinHeap = priority_queue<T, vector<T>, greater<T>>;

vector<int64_t> Dijkstra(const Graph& graph, int source) {
    vector<int64_t> dist(graph.size(), 1LL << 50);
    dist[source] = 0;

    MinHeap<pair<int64_t, int>> heap;
    heap.push({dist[source], source});

    while (!heap.empty()) {
        auto [d, node] = heap.top();
        heap.pop();

        if (d > dist[node]) {
            continue;
        }

        for (const auto& [other, cost] : graph[node]) {
            if (dist[node] + cost < dist[other]) {
                dist[other] = dist[node] + cost;
                heap.push({dist[other], other});
            }
        }
    }
    return dist;
}

bool Solve(const Graph& graph, int source, const vector<int64_t>& dist) {
    return dist == Dijkstra(graph, source);
}

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<int64_t> dist(nodes);
        for (auto& d : dist) {
            fin >> d;
        }

        auto 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});
        }

        auto res = Solve(graph, source - 1, dist);
        fout << (res ? "DA" : "NU") << "\n";
    }
    return 0;
}