Cod sursa(job #3162469)

Utilizator Maftei_TudorMaftei Tudor Maftei_Tudor Data 29 octombrie 2023 12:18:44
Problema Distante Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.42 kb
#include <fstream>
#include <vector>
#include <queue>

#define pb push_back
#define fi first
#define la second

using namespace std;
ifstream in("distante.in");
ofstream out("distante.out");
const int N = 5e4 + 5, M = 1e5 + 5, inf = 2e9;

int tt, n, m, st, rez[N], d[N];
bool is[N];
vector<pair<int, int>> g[N];
priority_queue<pair<int, int>> que;

int main()
{
    in >> tt;
    while(tt--) {
        in >> n >> m >> st;
        for(int i=1; i<=n; i++) {
            in >> rez[i];
            g[i].clear();
            d[i] = inf;
            is[i] = 0;
        }
        for(int j=1; j<=m; j++) {
            int x, y, lg;
            in >> x >> y >> lg;
            g[x].pb({lg, y});
            g[y].pb({lg, x});
        }

        d[st] = 0;
        que.push({0, st});
        while(!que.empty()) {
            int nod = que.top().la;
            que.pop();
            if(is[nod])
                continue;
            is[nod] = 1;

            for(auto nxt : g[nod])
                if(d[nxt.la] > d[nod] + nxt.fi) {
                    d[nxt.la] = d[nod] + nxt.fi;
                    que.push({-d[nxt.la], nxt.la});
                }
        }

        bool ok = 1;
        for(int i=1; i<=n; i++)
            if(rez[i] != d[i]) {
                ok = 0;
                break;
            }

        if(ok)
            out << "DA\n";
        else out << "NU\n";
    }
    return 0;
}