Cod sursa(job #2924049)

Utilizator AndreiBOTOBotocan Andrei AndreiBOTO Data 23 septembrie 2022 18:54:16
Problema Distante Scor Ascuns
Compilator cpp-64 Status waiting
Runda Arhiva de probleme Marime 1.47 kb
#include <iostream>
#include <fstream>
#include <algorithm>
#include <queue>
#include <vector>

using namespace std;

ifstream fin ("distante.in");
ofstream fout ("distante.out");

const int NMAX=1e5+5;
const int INF=2e9;

int dist[NMAX];
int viz[NMAX];
int sol[NMAX];

priority_queue<pair<int,int>>q;
vector<pair<int,int>>v[NMAX];

void dijkstra(int p)
{
    dist[p]=0;
    viz[p]=1;
    q.push(make_pair(0,p));
    while(!q.empty())
    {
        p=q.top().second;
        q.pop();
        viz[p]=0;
        for(auto i:v[p])
        {
            if(dist[p]+i.second<dist[i.first])
            {
                dist[i.first]=dist[p]+i.second;
                if(viz[i.first]==0)
                {
                    viz[i.first]=1;
                    q.push(make_pair(-dist[i.first],i.first));
                }
            }
        }
    }
}

int main()
{
    int t,n,m,i,j,a,b,c,s;
    fin>>t;
    while(t--)
    {
        bool ok=true;
        fin>>n>>m>>s;
        for(i=1;i<=n;i++)
            fin>>sol[i],dist[i]=INF;
        for(i=1;i<=m;i++)
        {
            fin>>a>>b>>c;
            v[a].push_back(make_pair(b,c));
        }
        dijkstra(s);
        for(i=1;i<=n;i++)
        {
            if(dist[i]==INF)
                dist[i]==0;
            if(dist[i]!=sol[i])
                ok=false;
        }
        if(ok)
            fout<<"DA\n";
        else
            fout<<"NU\n";
    }
    return 0;
}