Cod sursa(job #3377597)

Utilizator bexxRebeca N bexx Data 23 noiembrie 2026 18:29:10
Problema Distante Scor 10
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.36 kb
#include <fstream>
#include <vector>
#include <queue>
#include <bitset>
using namespace std;

ifstream cin("distante.in");
ofstream cout("distante.out");

int n,m,s;
bitset<50003>vis;
vector<long long>d,b;
vector<vector<pair<int,int>>>g;

auto cmp=[](int a, int b) {
    return d[a]>d[b];
};
priority_queue<int, vector<int>, decltype(cmp)> q(cmp);

void graph() {
    cin>>n>>m>>s;
    d.resize(n+1,5e7);
    b.resize(n+1);
    g.resize(n+1);
    for (int node=1;node<=n;node++)
        cin>>b[node];
    int x,y,c;
    for (int i=0;i<m;i++) {
        cin>>x>>y>>c;
        g[x].push_back({y,c});
        g[y].push_back({x,c});
    }
}

void dijkstra() {
    d[s]=0;
    q.push(s);
    while (!q.empty()) {
        int node=q.top();
        q.pop();
        if (vis[node])
            continue;
        vis[node]=1;
        if (d[node]!=b[node]) {
            cout<<"NU\n";
            return;
        }
        for (auto x : g[node])
            if (d[x.first]>d[node]+x.second) {
                d[x.first]=d[node]+x.second;
                q.push(x.first);
            }
    }
    cout<<"DA\n";
}

void clear() {
    for (int i=0;i<g.size();i++)
        g[i].clear();
    d.clear();
    b.clear();
    vis.reset();
    while (!q.empty())
        q.pop();
}
int main() {
    int t;
    cin>>t;
    while (t--) {
        graph();
        dijkstra();
        clear();
    }
    return 0;
}