Cod sursa(job #3038955)

Utilizator begusMihnea begus Data 27 martie 2023 22:34:38
Problema Distante Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.19 kb
#include <bits/stdc++.h>
using namespace std;

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

const int NMAX=5e4+1;

vector<pair<int, int>> adj[NMAX];
int t, n, m, s, dist[NMAX], distb[NMAX], processed[NMAX];

void solve(){
    fin >> n >> m >> s;
    for(int i=1; i<=n; i++)
        adj[i].clear();
    for(int i=1; i<=n; i++){
        processed[i]=false;
        dist[i]=INT_MAX/2;
        fin >> distb[i];
    }
    dist[s]=0;
    while(m--){
        int a, b, w;
        fin >> a >> b >> w;
        adj[a].push_back({b, w});
        adj[b].push_back({a, w});
    }
    priority_queue<pair<int, int>> q;
    q.push({0, s});
    while(!q.empty()){
        int a=q.top().second; q.pop();
        if(processed[a]) continue;
        processed[a]=true;
        for(auto u : adj[a]){
            int b=u.first, w=u.second;
            if(dist[b]>dist[a]+w){
                dist[b]=dist[a]+w;
                q.push({-dist[b], b});
            }
        }
    }
    bool good = true;
    for(int i=1; i<=n; i++) if(dist[i]!=distb[i]) good=false;
    if(good) fout << "DA\n";
    else fout << "NU\n";
}

int main(){
    fin >> t;
    while(t--) solve();
}