Cod sursa(job #3005135)

Utilizator SerbanCaroleSerban Carole SerbanCarole Data 16 martie 2023 19:40:16
Problema Distante Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.29 kb
#include <fstream>
#include <vector>
#include <queue>
using namespace std;

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

const int MAX = 5e4 + 1;

int temp[MAX] , dp[MAX] , n , m , st , x , y , c , t;

struct muchie{

    int y , c;
};

vector <muchie> g[MAX];

struct cmp{

    bool operator()( int a , int b ){

        return a > b;
    }
};

priority_queue <int,vector<int>,cmp> pq;

void testcase(){

    cin >> n >> m >> st;

    for(int i = 1 ; i <= n ; i++){

        g[i].clear();

        dp[i] = 1e9;

        cin >> temp[i];
    }

    for(int i = 1 ; i <= m ; i++){

        cin >> x >> y >> c;

        g[x].push_back({y,c});
        g[y].push_back({x,c});
    }

    dp[st] = 0;

    pq.push(st);

    while(!pq.empty()){

        x = pq.top();

        pq.pop();

        for(auto it : g[x]){

            if(dp[it.y] > dp[x] + it.c){

                dp[it.y] = dp[x] + it.c;

                pq.push(it.y);
            }
        }
    }

    for(int i = 1 ; i <= n ; i++){

        if(dp[i]!=temp[i]){

            cout << "NU" << '\n';

            return;
        }
    }

    cout << "DA" << '\n';
}

int main(){

    cin >> t;

    while(t--){

        testcase();
    }

    return 0;
}