Cod sursa(job #2923795)

Utilizator gargantuanRares Oprea gargantuan Data 19 septembrie 2022 11:03:22
Problema Distante Scor 30
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.44 kb
#include <fstream>
#include <vector>
#include <queue>
using namespace std;

ifstream cin("distante.in");
ofstream cout("distante.out");

const int nmax = 3e5;
int d[nmax],d2[nmax];
vector<vector<pair<int,int>>>adj(nmax);
priority_queue<pair<int,int>>pq;

int main()
{
    int t,n,m,s,i;
    cin >> t;
    while(t--)
    {
        cin >> n >> m >> s;
        for(i=1; i<=n; i++)
        {
            cin >> d2[i];
            d[i] = 1e9;
        }
        for(i=1; i<=m; i++)
        {
            int x,y,z;
            cin >> x >> y >> z;
            adj[x].push_back({z,y});
            adj[y].push_back({z,x});
        }
        pq.push({0,s});
        d[s] = 0;
        while(!pq.empty())
        {
            int node = pq.top().second;
            pq.pop();
            for(pair<int,int>p:adj[node])
            {
                //cout << p.first << " " << p.second <<  " " << d[node] << endl;
                if(d[p.second] > d[node] + p.first)
                {
                    d[p.second] = d[node] + p.first;
                    pq.push({d[node] + p.first,p.second});
                }
            }
        }
        int ok = 1;
        for(i=1; i<=n; i++)
            if(d[i] != d2[i])
                ok = 0;
        if(ok == 1)
            cout << "DA";
        else
            cout << "NU";
        for(i=1; i<=n; i++)
            adj[i].clear();
        cout << endl;
    }
    return 0;
}