Cod sursa(job #3221435)

Utilizator RobertAcAcatrinei Robert-Marian RobertAc Data 7 aprilie 2024 10:18:29
Problema Distante Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.63 kb
#include <bits/stdc++.h>
#ifdef LOCAL
#define in cin
#define out cout
#endif

using namespace std;

#ifndef LOCAL
ifstream in("distante.in");
ofstream out("distante.out");
#endif

const int nmax = 5e4 + 5;
const int inf = 1e9;

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

int n, m;
priority_queue<state> pq;
vector<pair<int, int>> adj[nmax];
bool viz[nmax];
int D[nmax];
int d[nmax];

bool dodij(int start)
{
    d[start] = 0;
    pq.push({start, d[start]});
    while (!pq.empty())
    {
        auto nod = pq.top();
        pq.pop();
        if (viz[nod.a])
            continue;
        viz[nod.a] = 1;
        if (d[nod.a] != D[nod.a])
        {
            return 0;
        }
        for (auto i : adj[nod.a])
        {
            if (d[i.first] > d[nod.a] + i.second)
            {
                d[i.first] = d[nod.a] + i.second;
                pq.push({i.first, d[i.first]});
            }
        }
    }
    for (int i = 1; i <= n; i++)
    {
        if (d[i] != D[i])
            return 0;
    }
    return 1;
}

void solve()
{
    int s;
    in >> n >> m >> s;
    while (!pq.empty())
        pq.pop();
    for (int i = 1; i <= n; i++)
    {
        in >> D[i];
        d[i] = inf;
        viz[i] = 0;
        adj[i].clear();
    }
    for (int i = 0; i < m; i++)
    {
        int a, b, c;
        in >> a >> b >> c;
        adj[a].push_back({b, c});
        adj[b].push_back({a, c});
    }
    if (dodij(s))
    {
        out << "DA\n";
    }
    else
    {
        out << "NU\n";
    }
}

int main()
{
    int t;
    in >> t;
    while (t--)
    {
        solve();
    }
}