Cod sursa(job #2797077)

Utilizator TghicaGhica Tudor Tghica Data 9 noiembrie 2021 11:09:13
Problema Distante Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.67 kb
#include <fstream>
#include <queue>


using namespace std;


int best[50001], rez[50001];

struct nodu {
    int nod, cost;
    bool operator < ( const nodu &other )const {
        return cost > other.cost;
    }
} aux2;

priority_queue<nodu>pq;

struct muchie {
    int dest, cost;
} aux;

vector<muchie>v[50001];

int main() {
    ifstream cin("distante.in");
    ofstream cout("distante.out");
    int n, m, i, a, b, c, t, start, ok;
    cin>>t;
    while( t-- ) {
        cin>>n>>m>>start;
        for( i = 1; i <= n; i++ ) {
          v[i].clear();
          best[i] = 0;
          cin>>rez[i];
        }
        for( i = 1; i <= m; i++ ) {
            cin>>a>>b>>c;
            aux.dest = b;
            aux.cost = c;
            v[a].push_back( aux );
        }
        aux2.nod = start;
        aux2.cost = 0;
        pq.push( aux2 );
        while( !pq.empty() ) {
            aux2 = pq.top();
            pq.pop();
            a = aux2.nod;
            c = aux2.cost;
            if( best[a] == 0 ) {
                best[a] = c;
                for( i = 0; i < v[a].size(); i++ ) {
                    if( best[v[a][i].dest] == 0 ) {
                        aux2.nod = v[a][i].dest;
                        aux2.cost = v[a][i].cost + c;
                        pq.push( aux2 );
                    }
                }
            }
        }
        best[start] = 0;
        ok = 0;
        for( i = 1; i <= n; i++ ) {
            if( best[i] != rez[i] ) {
              ok = 1;
            }
        }
        if( ok )
          cout<<"NU";
        else
          cout<<"DA";
        cout<<"\n";
    }
    return 0;
}