Cod sursa(job #2769854)

Utilizator stefanvoicaVoica Stefan stefanvoica Data 18 august 2021 09:22:53
Problema Distante Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.26 kb
#include <bits/stdc++.h>
#define dim 100002
#define int long long
using namespace std;
ifstream fin ("distante.in");
ofstream fout("distante.out");
int w[dim],sol[dim];
const int inf=100000000000000;

struct el
{
    int nod,cost;
    bool operator < (const el &A) const
    {
        return cost>A.cost;
    }
};
priority_queue<el>pq;
vector<el>a[dim];

void dijkstra()
{
    while (!pq.empty())
    {
        el x=pq.top();
        pq.pop();
        if (x.cost==sol[x.nod])
        for (auto y: a[x.nod])
            if (x.cost+y.cost<sol[y.nod])
        {
            sol[y.nod]=x.cost+y.cost;
            pq.push({y.nod,sol[y.nod]});
        }
    }
}

void Solve ()
{
    int i,n,m,s,x,y,z;
    fin>>n>>m>>s;
    for (i=1; i<=n; i++)
        fin>>w[i];
    while (m--)
    {
        fin>>x>>y>>z;
        a[y].push_back({x,z});
        a[x].push_back({y,z});
    }
    for (i=1; i<=n; i++)
        sol[i]=inf;
    sol[s]=0;
    pq.push({s,0});
    dijkstra();
    for (i=1; i<=n; i++)
        if (sol[i]!=w[i])
        {
            fout<<"NU"<<'\n';
            return;
        }
    fout<<"DA"<<'\n';
}

int32_t main()
{
    int t;
    fin>>t;
    while (t--)
    {
        Solve();
    }
    return 0;
}