Cod sursa(job #3378843)

Utilizator maria_avramAvram Maria Teodora maria_avram Data 28 noiembrie 2026 15:40:45
Problema Distante Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.67 kb
#include <bits/stdc++.h>
using namespace std;
ifstream f("distante.in");
ofstream g("distante.out");
const int NMAX = 50005, INF = 0x3F3F3F3F;
int t, n, m, s,d;
vector<int> dist, dist_init;
vector<vector<pair<int, int>>> graph;
bitset<NMAX> used;
void dijkstra(int node)
{
    priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
    dist[node] = 0;
    pq.push({0, node});

    while (!pq.empty())
    {
        node = pq.top().second;
        pq.pop();

        if (!used[node])
        {
            for (auto &[neighbour, cost] : graph[node])
                if (dist[node] + cost < dist[neighbour])
                {
                    dist[neighbour] = dist[node] + cost;
                    pq.push({dist[neighbour], neighbour});
                }
            used[node] = 1;
        }
    }
}
int main()
{
    f >> t;
    for (int k = 1; k <= t; k++) {
        f >> n >> m >> s;
        dist_init.assign(n + 1, 0);
        dist.assign(n + 1, INF);
        graph.assign(n + 1, vector<pair<int, int>>());
        used.reset();
        for (int j = 1; j <= n; j++) {
            f >> d;
            dist_init[j] =d;
        }

        for (int i = 1; i <= m; i++)
        {
            int x, y, c;
            f >> x >> y >> c;
            graph[x].push_back({y, c});
            graph[y].push_back({x, c});
        }

        dijkstra(s);

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

        if (ok)
            g << "DA" << endl;
        else
            g << "NU" << endl;

    }

    return 0;
}