Cod sursa(job #3355163)

Utilizator Emilia23Dobra Emilia Emilia23 Data 21 mai 2026 21:44:45
Problema Distante Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.35 kb
#include <bits/stdc++.h>

using namespace std;
ifstream f("distante.in");
ofstream g("distante.out");
int d[50002], dist[50002];

int main() {
    int t, n, m, x, y, z, s;
    f >> t;
    while (t--) {
        f >> n >> m >> s;
        for (int i = 1; i <= n; i++) {
            f >> d[i];
            dist[i] = 1e8;
        }
        vector<vector<pair<int, int>>> adj(n + 1);
        priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
        for (int i = 1; i <= m; i++) {
            f >> x >> y >> z;
            adj[x].push_back({y, z});
            adj[y].push_back({x, z});
        }
        dist[s] = 0;
        pq.push({0, s});
        while (!pq.empty()) {
            auto u = pq.top();
            int node = u.second, cost = u.first;
            pq.pop();
            if (dist[node] < cost) continue;
            for (auto [neigh, c] : adj[node]) {
                if (dist[neigh] > dist[node] + c) {
                    dist[neigh] = dist[node] + c;
                    pq.push({dist[neigh], neigh});
                }
            }
        }
        bool ok = true;
        for (int i = 1; i <= n; i++) {
            if (d[i] != dist[i]) {
                ok = false;
                break;
            }
        }
        if (ok) g << "DA" << '\n';
        else g << "NU" << '\n';
    }
}