Cod sursa(job #3335384)

Utilizator GoreaRaresGorea Rares-Andrei GoreaRares Data 22 ianuarie 2026 16:40:26
Problema Distante Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.4 kb
#include <bits/stdc++.h>
#define inf INT_MAX

using namespace std;

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

struct state{
    int node, cost;
    bool operator < (const state & other) const{
        return cost > other.cost;
    }
};

int t, n, m, s, d[50001], dist[50001];
vector<state> adj[50001];
priority_queue<state> pq;

void read(){
    int x, y, c;
    fin >> n >> m >> s;
    for(int i = 1; i <= n; i++)
        fin >> d[i];
    for(int i = 1; i <= m; i++){
        fin >> x >> y >> c;
        adj[x].push_back({y, c});
        adj[y].push_back({x, c});
    }
}

bool check(){
    for(int i = 1; i <= n; i++)
        if(dist[i] != d[i])
            return false;
    return true;
}

void dijkstra(int w){
    dist[w] = 0;
    pq.push({w, 0});
    while(!pq.empty()){
        auto [u, costu] = pq.top();
        pq.pop();
        if(costu != dist[u]) continue;
        for(auto [v, costv] : adj[u])
            if(dist[u] + costv < dist[v])
                dist[v] = dist[u] + costv, pq.push({v, dist[v]});
    }
}

void processQueries(){
    fin >> t;
    while(t--){
        read();
        for(int i = 1; i <= n; i++)
            dist[i] = inf;
        dijkstra(s);
        fout << (check() ? "DA\n" : "NU\n");
        for(int i = 1; i <= n; i++)
            adj[i].clear();
    }
}

int main()
{
    processQueries();
    return 0;
}