Cod sursa(job #3377224)

Utilizator matei__bBenchea Matei matei__b Data 22 noiembrie 2026 12:55:56
Problema Distante Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.13 kb
// https://infoarena.ro/problema/distante

#include <bits/stdc++.h>
using namespace std;

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

const int NMAX=5e4+8;

int n,m,src;
vector<pair<int,int> > g[NMAX];
int dd[NMAX], dist[NMAX];

void solve() {
    fin >> n >> m >> src;
    for(int i=1; i<=n; i++) {
        g[i].clear();
        fin >> dd[i];
        dist[i]=INT_MAX;
    }

    for(int i=1; i<=m; i++) {
        int x,y,w;
        fin >> x >> y >> w;
        g[x].push_back({y,w});
        g[y].push_back({x,w});
    }

    dist[src]=0;
    queue<int> q; q.push(src);

    while(!q.empty()) {
        int nod=q.front();
        q.pop();

        for(const auto &[vec,w]:g[nod]) {
            if(dist[vec]>w+dist[nod]) {
                dist[vec]=w+dist[nod];
                q.push(vec);
            }
        }
    }

    bool ok=1;
    for(int i=1; i<=n; i++) {
        ok&=(dist[i]==dd[i]);
    }

    fout << (ok ? "DA\n" : "NU\n");
}

int main() {
    ios_base::sync_with_stdio(false);
    fin.tie(nullptr);
    fout.tie(nullptr);

    int tests;
    fin >> tests;
    for(int i=1; i<=tests; i++) {
        solve();
    }

    return 0;
}