Cod sursa(job #2923336)

Utilizator andreibrosPeta Andrei Mathias andreibros Data 12 septembrie 2022 20:33:27
Problema Distante Scor 50
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.68 kb
#include <iostream>
#include <fstream>
#include <queue>
#include <vector>
#include <climits>
using namespace std;
ifstream in ("distante.in");
ofstream out("distante.out");
vector <pair<int,int>> graf[50001];
priority_queue <pair< int,int>> h;
int v[50001],dist[50001];
bool inq[50001];
int main()
{
    int T,n,m,s,x,y,cost;
    in>>T;
    for(int l=1; l<=T; l++)
    {

        in>>n>>m>>s;


        for(int i=1; i<=n; i++)
        {
            in>>v[i];
            dist[i]=INT_MAX;
        }
        for(int i=1; i<=m; i++)
        {
            in>>x>>y>>cost;
            graf[x].push_back(make_pair(y,cost));
            graf[y].push_back(make_pair(x,cost));
        }
        h.push(make_pair(s,0));
        dist[s]=0;
        inq[s]=1;
        while(h.empty()==false)
        {
            x=h.top().first;
            inq[x]=0;
            h.pop();
            for(unsigned int i=0; i<graf[x].size(); i++)
            {
                y=graf[x][i].first;
                cost=graf[x][i].second;
                if(dist[y]>dist[x]+cost)
                {
                    dist[y]=dist[x]+cost;
                    if(inq[y]==0)
                    {

                        h.push(make_pair(y,-dist[y]));
                        inq[y]=1;
                    }

                }


            }
        }


        bool ok=1;
        for(int i=1; i<=n && ok==1; i++)
        {
            if(dist[i]!=v[i])
                ok=0;


        }
        if(ok==0)
            out<<"NU"<<'\n';
        else
            out<<"DA"<<'\n';
        for(int i = 1; i <= n; i++)
            graf[i].clear();

    }

    return 0;
}