Cod sursa(job #3355801)

Utilizator VladStroicaStroica Vlad Cristian VladStroica Data 26 mai 2026 10:59:23
Problema Distante Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.6 kb
#include <bits/stdc++.h>

using namespace std;
int dist[100001];
vector<pair<int,int>> g[50005];
int d[50005];

void dij(int n)
{
    priority_queue<pair<int,int>> q;
    q.push({0,n});
    d[n]=0;
    while(!q.empty())
    {
        int h=q.top().second;
        int j=q.top().first*-1;
        q.pop();
        if(d[h]==j)
        {
            for(int i=0;i<g[h].size();i++)
            {
                int cost=j+g[h][i].second;
                if(cost<d[g[h][i].first])
                {
                    d[g[h][i].first]=cost;
                    q.push({cost*-1,g[h][i].first});
                }
            }
        }
    }
}

int main() {
    ifstream cin("distante.in");
    ofstream cout("distante.out");
    int t;
    cin>>t;
    for(int ni=1;ni<=t;ni++)
    {
        int n,m,s;
        cin>>n>>m>>s;
        for(int i=1;i<=n;i++)
        {
            g[i].clear();
        }
        for(int i=1;i<=n;i++)
        {
            d[i]=INT_MAX;
        }
        for(int i=1;i<=n;i++)
        {
            cin>>dist[i];
        }
        for(int i=1;i<=m;i++)
        {
            int x,y,z;
            cin>>x>>y>>z;
            g[x].push_back({y,z});
            g[y].push_back({x,z});
        }
        dij(s);
        int ok=0;
        for(int i=1;i<=n;i++)
        {
            if(d[i]!=dist[i])
            {
                ok=1;
                break;
            }
        }
        if(ok==1)
        {
            cout<<"NU\n";
        }
        else
        {
            cout<<"DA\n";
        }

    }

    return 0;
}