Cod sursa(job #2819405)

Utilizator Ionut151Marcu Ionut Ionut151 Data 18 decembrie 2021 12:12:23
Problema Distante Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.62 kb
#include <iostream>
#include <algorithm>
#include <functional>
#include <queue>
#include <vector>
#include <fstream>
#include <cstring>

using namespace std;

#define PAIR pair<int,int>

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

const int inf = 1e9;
int n,m,s,t;
int d[50001];
int d_check[50001];
vector<vector<PAIR> > g(50001);
priority_queue<PAIR, vector<PAIR>,greater<PAIR>> pq;
int v[50001];

void djk(int start){
    for(int i=1;i<=n;i++)
        d[i] = inf;
    d[start] = 0;
    pq.push({0,start});
    while(!pq.empty()){
        PAIR p = pq.top();
        int dist,nod;
        dist = p.first;
        nod = p.second;
        pq.pop();
        if(dist > d[nod])
            continue;

        for(auto next: g[nod]){
            if(d[next.first] > dist + next.second){
                d[next.first] = dist + next.second;
                pq.push({d[next.first],next.first});
            }
        }
    }
}

int main()
{
    in >> t;
    for(int k=1;k<=t;k++){
        in >> n >> m >> s;
        for(int i=1;i<=n;i++){
            in >> d_check[i];
        }
        g.clear();
        for(int i=1;i<=m;i++){
            int a,b,c;
            in >> a >> b >> c;
            g[a].push_back(make_pair(b,c));
        }
        djk(s);
        int f = 1;
        for(int i=1;i<=n;i++){
            if(d[i] == inf)
                d[i]=0;
            if(d_check[i] != d[i]){
                f= 0;
                break;
            }
        }
        if(f){
            out << "DA\n";
        } else {
            out << "NU\n";
        }
    }
    return 0;
}