Cod sursa(job #3229736)

Utilizator Alex_BerbescuBerbescu Alexandru Alex_Berbescu Data 17 mai 2024 11:01:28
Problema Distante Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.65 kb
#pragma GCC optimize("O3")
#pragma GCC optimize("fast-math")
#pragma GCC optimize("unroll-loops")
#include<bits/stdc++.h>
#define inf 0x3f3f3f3f
using namespace std;
const int dim = 5e4 + 5;
int test_cases, ok, n, m, source, v[dim], x, y, z;
struct el
{
    int nodu, cost;
    bool operator<(const el& ob)const
    {
        return cost > ob.cost;
    }
};
ifstream fin("distante.in");
ofstream fout("distante.out");
int32_t main(int argc, char * argv[])
{
    fin >> test_cases;
    while(test_cases--)
    {
        ok = 1;
        priority_queue<el>pq;
        vector<pair<int, int> >noduri[dim];
        fin >> n >> m >> source;
        int dist[dim];
        for(int i = 1; i <= n; ++i)
        {
            dist[i] = inf;
        }
        dist[source] = 0;
        for(int i = 1; i <= n; ++i)
        {
            fin >> v[i];
        }
        for(int j = 1; j <= m; ++j)
        {
            fin >> x >> y >> z;
            noduri[x].push_back({y, z});
            noduri[y].push_back({x, z});
        }
        pq.push({source, 0});
        while(!pq.empty())
        {
            int x = pq.top().nodu;
            int costu = pq.top().cost;
            pq.pop();
            for(auto it: noduri[x])
            {
                if(costu + it.second < dist[it.first])
                {
                    dist[it.first] = costu + it.second;
                    pq.push({it.first, dist[it.first]});
                }
            }
        }
        for(int i = 1; i <= n; ++i)
        {
            if(dist[i] != v[i])
            {
                ok = 0;
                break;
            }
        }
        fout << ((ok == 1)?"DA":"NU") << '\n';

    }
    return 0;
}