Cod sursa(job #3257506)

Utilizator Sasha_12454Eric Paturan Sasha_12454 Data 17 noiembrie 2024 23:19:19
Problema Distante Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.39 kb
#include <bits/stdc++.h>
using namespace std;

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

int t, n, m, k, a[50005], dist[50005];
bool viz[50005];

void solve()
{
    bool adv = true;
    fin >> n >> m >> k;
    for(int i = 1; i <= n; ++i){
        fin >> a[i];
        viz[i] = false;
        dist[i] = 50000005;
    }
    vector<pair<int, int>> v[50005];
    priority_queue<pair<int, int>> pq;
    for(int i = 1; i <= m; ++i){
        int x, y, c;
        fin >> x >> y >> c;
        v[x].push_back(make_pair(y, c));
        v[y].push_back(make_pair(x, c));
    }
    dist[k] = 0;
    pq.push(make_pair(0, k));
    while(!pq.empty()){
        int nod = pq.top().second;
        pq.pop();
        if(!viz[nod]){
            for(auto i : v[nod]){
                if(dist[nod]+i.second<dist[i.first]){
                    dist[i.first] = dist[nod] + i.second;
                    pq.push(make_pair(-dist[i.first], i.first));
                }
            }
            viz[nod] = true;
        }
    }
    for(int i = 1; i <= n; ++i){
        if(dist[i] != a[i]){
            adv = false;
        }
    }
    if(adv){
        fout << "DA" << '\n';
    }
    else{
        fout << "NU" << '\n';
    }
}

int main(){
    /*ios_base::sync_with_stdio(false);
    cin.tie(nullptr);*/
    fin >> t;
    while(t--){
        solve();
    }
    return 0;
}