Cod sursa(job #2536015)

Utilizator rares9991Matisan Rares-Stefan rares9991 Data 1 februarie 2020 13:44:23
Problema Distante Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.4 kb
#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 lst[N], vf[M], urm[M], q[N+5], nr, d[N], cst[M], nrt, t[N], dr=-1, st,x,y,c,n,m,s;
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(0,x0));
    while(!h.empty())
    {
        while(!h.empty() and 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));
            }
        }
    }
}

int main()
{
    in>>nrt;
    for(int r=1; r<=nrt; r++)
    {
        in>>n>>m>>s;
        for(int i=1; i<=n; i++)
            in>>t[i];
        bool ok=true;
        for(int i=1; i<=m; i++)
        {
            in>>x>>y>>c;
            if(t[x]+c<t[y] or t[y]+c<t[x])
                ok=false;
        }
        if(ok==false or t[s]!=0)
            out<<"NU";
        else
            out<<"DA";
    }
    return 0;

}