Cod sursa(job #2999424)

Utilizator Theodor17Pirnog Theodor Ioan Theodor17 Data 10 martie 2023 23:31:27
Problema Distante Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.63 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];
bool viz[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;
    viz[x] = true;
    q.push({0, x});

    while(!q.empty()){

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

        q.pop();
        viz[x] = false;

        for(auto y : g[x]){

            int c = ci + y.second;

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

                cost[y.first] = c;

                if(viz[y.first] == false){

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

                }
            }
        }

    }

}


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++){

            ok = (cost[i] == v[i]);
            viz[i] = false;
            g[i].clear();

        }

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


    }

    cin.close();
    cout.close();

    return 0;
}