Cod sursa(job #2970303)

Utilizator AleXutzZuDavid Alex Robert AleXutzZu Data 24 ianuarie 2023 21:07:59
Problema Distante Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.87 kb
#include <iostream>
#include <fstream>
#include <queue>
#include <vector>

std::vector<int> dijkstra(int source, const std::vector<std::vector<std::pair<int, int>>> &graph) {
    std::vector<int> dist(graph.size(), INT32_MAX);
    dist[source] = 0;

    auto cmp = [](const std::pair<int, int> &_lhs, const std::pair<int, int> &_rhs) {
        return _lhs.second > _rhs.second;
    };

    std::priority_queue<std::pair<int, int>, std::vector<std::pair<int, int>>, decltype(cmp)> queue(cmp);

    queue.emplace(source, 0);

    while (!queue.empty()) {
        auto top = queue.top();
        queue.pop();
        if (top.second != dist[top.first]) continue;

        for (const auto &x: graph[top.first]) {
            int candidate = dist[top.first] + x.second;

            if (candidate < dist[x.first]) {
                dist[x.first] = candidate;
                queue.emplace(x.first, candidate);
            }
        }
    }
    int n = graph.size();
    for (int i = 1; i < n; ++i) {
        if (dist[i] == INT32_MAX) dist[i] = 0;
    }

    return dist;
}


int main() {
    std::ifstream input("distante.in");
    std::ofstream output("distante.out");
    int t;
    input >> t;

    std::vector<std::vector<std::pair<int, int>>> graph;
    std::vector<int> cnd;
    while (t--) {
        int n, m, s;
        input >> n >> m >> s;

        cnd.resize(n + 1);
        graph.resize(n + 1);

        for (int i = 1; i <= n; ++i) input >> cnd[i];

        while (m--) {
            int x, y, p;
            input >> x >> y >> p;
            graph[x].emplace_back(y, p);
            graph[y].emplace_back(x, p);
        }

        auto correct = dijkstra(s, graph);
        bool good = true;

        for (int i = 1; i <= n; ++i) {
            if (correct[i] != cnd[i]) good = false;
        }
        output << (good ? "DA" : "NU") << '\n';
        cnd.clear();
        graph.clear();
    }
    return 0;
}