Cod sursa(job #2536008)

Utilizator DanSDan Teodor Savastre DanS Data 1 februarie 2020 13:36:03
Problema Distante Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.66 kb
#include <iostream>
#include <fstream>
#include <queue>

using namespace std;

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

const int N = 50001;
const int M = 250001;
const int INF = 1e9;
int n, m, st, dr = -1, nr, x, y, c, t, s, d[N], lst[N], vf[M], urm[M], cst[M], q[N+2], v[N];
bool sel[N];

priority_queue< pair<int, int> > h;

void adauga(int x, int y, int c)
{
    vf[++nr] = y;
    cst[nr] = c;
    urm[nr] = lst[x];
    lst[x] = nr;
}

void dijkstra(int x0)
{
    for(int i=1; i<=n; i++)
    {
        d[i] = INF;
        sel[i] = false;
    }
    d[x0] = 0;
    h.push(make_pair(d[x0], x0));
    while(!h.empty())
    {
        while(!h.empty() && sel[h.top().second])
            h.pop();
        if(h.empty())
            return;
        x = h.top().second;
        sel[x] = true;
        for(int p = lst[x]; p != 0; p = urm[p])
        {
            y = vf[p];
            c = cst[p];
            if(d[x] + c < d[y]){
                d[y] = d[x] + c;
                h.push(make_pair(-d[y], y));
            }
        }
    }
}

void Reset()
{
    st = 0;
    dr = -1;
    nr = 0;
    for(int i=1; i<=m; i++)
        lst[i] = vf[i] = urm[i] = cst[i] = 0;

}

int main()
{
    in>>t;
    for(int i=1; i<=t; i++)
    {
        Reset();

        in>>n>>m>>s;
        for(int i=1; i<=n; i++)
            in>>v[i];
        for(int i=1; i<=m; i++)
        {
            in>>x>>y>>c;
            adauga(x, y, c);
        }
        dijkstra(s);
        bool ok = true;
        for(int i=1; i<=n; i++)
        {
            if(d[i] == INF) d[i] = 0;
            if(v[i] != d[i]) ok = false;
        }
        if(ok) out<<"DA"<<'\n';
        else out<<"NU"<<'\n';
    }
    return 0;
}