Cod sursa(job #2923331)

Utilizator andreibrosPeta Andrei Mathias andreibros Data 12 septembrie 2022 20:05:42
Problema Distante Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.6 kb
#include <iostream>
#include <fstream>
#include <queue>
#include <vector>
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 visited[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]=-1;
        }
        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;
        while(h.empty()==false)
        {
            while(h.empty()==false && visited[h.top().first]==1 )
            {
                h.pop();
            }
            if(h.empty()==true)
                break;
            x=h.top().first;
            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]==-1)
                {
                    dist[y]=dist[x]+cost;
                    h.push(make_pair(y,-dist[y]));
                }
            }
        }
        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';
    }

    return 0;
}