Cod sursa(job #2762548)

Utilizator Bulboaca_EugenBulboaca Alexandru Eugen Bulboaca_Eugen Data 8 iulie 2021 10:32:42
Problema Distante Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.49 kb
#include <bits/stdc++.h>
using namespace std;

const int MAXN =  5 * 1e4 + 15;
const int INF = 1e8;

struct edge{

    int dest, cost;
    bool operator < (const edge &aux) const{
        return cost > aux.cost;
    }

};

int dist[MAXN], v[MAXN];
priority_queue <edge> pq;
vector <edge> g[MAXN];

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

void dijkstra(){

    int n, m, nod; fin >> n >> m >> nod;
    for(int i = 1; i <= n; ++i){
        fin >> v[i];
        dist[i] = INF;
    }
    for(int i = 1; i <= m ; ++i){
        int x, y, cost; fin >> x >> y >> cost;
        g[x].push_back({y, cost});
        g[y].push_back({x, cost});
    }

    pq.push({nod, 0});
    while(pq.size()){

        edge x = pq.top();
        pq.pop();

        if(dist[x.dest] == INF){
            dist[x.dest] = x.cost;

            for(auto y : g[x.dest]){
                if(dist[y.dest] == INF )
                    pq.push({y.dest, dist[x.dest] + y.cost});
            }
        }
    }
        for(int i = 1; i <= n; ++i)
            while(g[i].size()) g[i].pop_back();
        while(pq.size()) pq.pop();

        bool flag = false;
        for(int i = 1; i <= n; ++i){
            if(dist[i] >= INF) dist[i] = 0;
            if(dist[i] != v[i]) flag = 1;
        }
        if(flag) fout << "NU" << '\n';
        else fout << "DA" << '\n';

}

int main(){

    int n; fin >> n;
    for(int i = 1; i <= n; ++i){
        dijkstra();
    }
    return 0;
}