Cod sursa(job #2999435)

Utilizator Theodor17Pirnog Theodor Ioan Theodor17 Data 10 martie 2023 23:45:02
Problema Distante Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.43 kb
#include <fstream>
#include <vector>
#include <queue>


using namespace std;

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

const int NMAX = 5e4;
const int inf = 2e9;

int v[NMAX + 1], cost[NMAX + 1];
vector <pair <int, int>> g[NMAX + 1];
priority_queue <pair <int, int>, vector <pair <int, int>>, greater <pair <int, int>>> q;

void dijkstra(int x){

    cost[x] = 0;
    q.push({0, x});

    while(!q.empty()){

        int ci = q.top().first;
        int x = q.top().second;

        q.pop();


        for(auto y : g[x]){

            int c = cost[x] + y.second;

            if(cost[y.first] > c){

                cost[y.first] = c;
                q.push({cost[y.first], y.first});

            }
        }

    }

}


int main(){

    int teste = 0;
    cin >> teste;

    int n = 0, m = 0, init = 0;
    while(teste--){

        cin >> n >> m >> init;

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

            cin >> v[i];
            cost[i] = inf;

        }

        int x = 0, y = 0, c = 0;
        for(int i = 0; i < m; i++){

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

        }

        dijkstra(init);

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

            if(cost[i] != v[i])
                ok = false;

            g[i].clear();

        }

        if(ok == true)
            cout << "DA\n";
        else
            cout << "NU\n";


    }

    return 0;
}