Cod sursa(job #3002257)

Utilizator AndreiMargaritMargarit Andrei AndreiMargarit Data 14 martie 2023 16:20:23
Problema Distante Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.7 kb
#include <bits/stdc++.h>
#define infinit 1000000001

using namespace std;

ifstream f("distante.in");
ofstream g("distante.out");

vector <pair<int,int>>a[50001];

priority_queue <pair <int,int>,
    vector <pair <int,int> > , greater <pair <int,int> > > pq;

int d[50001],d2[50001],viz[50001];

void dijkstra(int s)
{
    int nodbun,i,vecin,cost,cost2;
    pq.push({0,s});
    d[s]=0;
    while(!pq.empty())
    {
        nodbun=pq.top().second;
        cost=pq.top().first;
        pq.pop();
        if(viz[nodbun]==0)
        {
            viz[nodbun]=1;
            for(i=0;i<a[nodbun].size();i++)
            {
                vecin=a[nodbun][i].first;
                cost2=a[nodbun][i].second;
                if(viz[vecin]==0&&d[vecin]>cost+cost2)
                {
                    d[vecin]=cost+cost2;
                    pq.push({d[vecin],vecin});
                }
            }
        }
    }
}

int main()
{int t,n,m,s,i,k,x,y,cost,ok;
    f>>t;
    for(k=1;k<=t;k++)
    {
        f>>n>>m>>s;
        for(i=1;i<=n;i++)
        {
            f>>d2[i];
            d[i]=infinit;
        }
        for(i=1;i<=m;i++)
        {
            f>>x>>y>>cost;
            a[x].push_back({y,cost});
            a[y].push_back({x,cost});
        }
        dijkstra(s);
        ok=1;
        for(i=1;i<=n;i++)
        {
            if(d[i]!=d2[i])
            {
                ok=0;
            }
        }
        if(ok)
        {
            g<<"DA"<<'\n';
        }
        else
        {
            g<<"NU"<<'\n';
        }
        for(i=1;i<=n;i++)
        {
            a[i].clear();
            viz[i]=0;
        }
    }
    return 0;
}