Cod sursa(job #2766555)

Utilizator pielevladutPiele Vladut Stefan pielevladut Data 2 august 2021 11:36:24
Problema Distante Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.5 kb
#include <bits/stdc++.h>

using namespace std;

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

int t;
int n, m, s;
int d[50505];
int ans[50505];
struct elem
{
    int nod, cost;
    bool operator < (const elem other) const
    {
        return cost > other.cost;
    }
};
vector<elem> v[50505];
priority_queue<elem> coada;
void Dijkstra(int nod)
{
    coada.push({nod, 0});
    while(!coada.empty())
    {
        int nod = coada.top().nod;
        int cost = coada.top().cost;
        coada.pop();
        if(cost != ans[nod])
            continue;
        for(auto it : v[nod])
        {
            if(cost + it.cost < ans[it.nod])
            {
                ans[it.nod] = cost + it.cost;
                coada.push({it.nod, ans[it.nod]});
            }
        }
    }
}

int main()
{
    fin >> t;
    while(t--)
    {
        fin >> n >> m >> s;
        for(int i = 1; i <= n; i ++)
        {
            v[i].clear();
            fin >> d[i];
            ans[i] = INT_MAX;
        }
        ans[s] = 0;
        while(m--)
        {
            int a, b, c;
            fin >> a >> b >> c;
            v[a].push_back({b,c});
            v[b].push_back({a,c});
        }
        Dijkstra(s);
        bool ok = true;
        for(int i = 1; i <= n; i ++)
        {
            if(ans[i] != d[i])
                ok = false;
        }
        if(ok)
            fout << "DA";
        else
            fout << "NU";
        fout << '\n';
    }
}