Cod sursa(job #2823561)

Utilizator Alex18maiAlex Enache Alex18mai Data 28 decembrie 2021 20:52:49
Problema Distante Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.83 kb
/*
Alexandru Enache
Grupa 252
*/


#include <bits/stdc++.h>

using namespace std;

//ifstream fin("input"); ofstream fout("output");
ifstream fin("distante.in");ofstream fout("distante.out");


const int inf = 1e9;

void solve() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    int tests;
    fin >> tests;

    while (tests--){
        int n, m, source;
        fin >> n >> m >> source;

        vector < int > expected_answer(n + 1);
        vector< vector < pair <int, int> > > gr(n + 1);

        for (int i=1; i<=n; i++){
            fin>>expected_answer[i];
        }

        for (int i = 1; i <= m; i++) {
            int a, b, value;
            fin >> a >> b >> value;
            gr[a].push_back({b, value});
            gr[b].push_back({a, value});
        }

        vector<int> dp(n + 1, inf);

        priority_queue<pair<int, int>, vector<pair<int, int>>, greater<>> Q;
        vector<bool> used(n + 1, false);

        dp[source] = 0;
        Q.push({0, source});

        while (!Q.empty()) {
            pair<int, int> current_node = Q.top();
            Q.pop();
            if (used[current_node.second] || current_node.first != dp[current_node.second]) {
                continue;
            }
            used[current_node.second] = true;
            for (auto &x : gr[current_node.second]) {
                if (dp[x.first] > current_node.first + x.second) {
                    dp[x.first] = current_node.first + x.second;
                    Q.push({dp[x.first], x.first});
                }
            }
        }

        bool ok = true;
        for (int i=1; i<=n; i++){
            if (dp[i] != expected_answer[i]){
                ok = false;
            }
        }

        if (ok)
            fout<<"DA\n";
        else
            fout<<"NU\n";

    }

}


int main() {

    solve();

    return 0;
}