Cod sursa(job #2709811)

Utilizator ANNOnymousMihaila Stefan-Alexandru ANNOnymous Data 21 februarie 2021 12:44:25
Problema Distante Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.42 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");

const int Inf=(1<<30);
const int Nmax=50001;

using PI=pair<int, int>;

int v[Nmax];
int n, m, s, x, y, w, t;
int D[Nmax];
vector<PI>G[Nmax];

void dijkstra(int nod)
{
    priority_queue< PI, vector<PI>, greater<PI>>Q;
    D[nod]=0;
    Q.push({0, nod});
    while(!Q.empty())
    {
        x=Q.top().first;
        y=Q.top().second;
        Q.pop();
        if(x>D[y])continue;
        for(auto& q:G[y])
        {
            int nodnou=q.first;
            int costnou=q.second;
            if(D[nodnou]>D[y]+costnou)
            {
                D[nodnou]=D[y]+costnou;
                Q.push({D[nodnou], nodnou});
            }
        }
    }
}

int main()
{
    fin>>t;
    while(t--)
    {
        fin>>n>>m>>s;
        for(int i=1; i<=n; i++)
            fin>>v[i];
        while(m--)
        {
            fin>>x>>y>>w;
            G[x].push_back({y, w});
            G[y].push_back({x, w});
        }
        for(int i=1; i<=n; i++)
        {
            D[i]=Inf;
        }
        dijkstra(s);
        bool ok=true;
        for(int i=1; i<=n; i++)
        {
            if(D[i]!=v[i])
            {
                ok=false;
            }
        }
        if(ok)
            fout<<"DA";
        else
            fout<<"NU";
        fout<<'\n';
    }
}