Cod sursa(job #2841382)

Utilizator Botnaru_VictorBotnaru Victor Botnaru_Victor Data 29 ianuarie 2022 17:39:07
Problema Distante Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.7 kb
#include <bits/stdc++.h>
#define nmax 500001
using namespace std;

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


struct ed
{
    int e,c;
    ed(int e=0,int c=0)
    {
        this->e=e;
        this->c=c;
    }
};


vector<ed> adj[nmax]; //de ref
int n,m,s;
int arb[4*nmax];
int dij[nmax],val[nmax];


int v[nmax];

void update(int nod,int st, int dr, int up, int uv)
{
    if(up<st||up>dr) return;
    if(st==dr&&st==up)
    {
        arb[nod]=up;
        val[arb[nod]]=uv;
        return;
    }
    int mid=(st+dr)/2;
    update(nod*2,st,mid,up,uv);
    update(nod*2+1,mid+1,dr,up,uv);
    arb[nod]=val[arb[nod*2]]>val[arb[nod*2+1]]?arb[nod*2+1]:arb[nod*2];
}

void dk(int st)
{
    for(int i=1;i<=n;i++)
    {
        update(1,1,n,i,INT_MAX);
        dij[i]=INT_MAX;
    }
    val[0]=INT_MAX;
    update(1,1,n,st,0);
    while(val[arb[1]]!=INT_MAX)
    {
        //cout<<"here"<<' '<<arb[1]<<'\n';
        int c=arb[1];
        dij[c]=val[c];
        update(1,1,n,c,INT_MAX);
        for(auto e:adj[c])
        {
            if(dij[e.e]==INT_MAX&&val[e.e]>dij[c]+e.c)
            {
                update(1,1,n,e.e,dij[c]+e.c);
            }
        }
    }
}


int main()
{
    int t; f>>t;
    while(t--)
    {
        f>>n>>m>>s;
        for(int i=1;i<=n;i++) f>>v[i];
        for(int i=0;i<m;i++)
        {
            int a,b,c;
            f>>a>>b>>c;
            adj[a].push_back(ed(b,c));
            adj[b].push_back(ed(a,c));
        }
        dk(s);
        bool ok=1;
        for(int i=1;i<=n;i++)
        {
            if(v[i]!=dij[i]) ok=0;
        }
        if(ok) g<<"DA\n";
        else g<<"NU\n";
    }
    return 0;
}