Cod sursa(job #2587054)

Utilizator GiosinioGeorge Giosan Giosinio Data 21 martie 2020 22:25:30
Problema Distante Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.7 kb
#include <bits/stdc++.h>
#define DIM 50005
#define oo (2 << 29)

using namespace std;

ifstream f("distante.in");
ofstream g("distante.out");

struct node{
    int v, c;
    node *next;
};

int T,N,M,S,db[DIM],d[DIM];
bool InQueue[DIM];

struct comparison{
    bool operator() (int x, int y){
        return d[x] > d[y];
    }
};

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

void add(int x, int y, int c, node *L[]){
    node *p = new node;
    p->v = y, p->c = c;
    p->next = L[x];
    L[x] = p;
}

void citire(int &N, int &M, int &S, int db[], node *L[]){
    f>>N>>M>>S;
    for(int i=1; i<=N; i++)
        f>>db[i];
    int a,b,c;
    for(int i=1; i<=M; i++){
        f>>a>>b>>c;
        add(a,b,c,L);
        add(b,a,c,L);
    }
}

bool check(int n, int d[], int db[]){
    for(int i=1; i<=n; i++)
        if(d[i] != db[i]) return 0;
    return 1;
}

void Dijkstra_Heap(int N, int S, int db[], node *L[]){
    for(int i=1; i<=N; i++)
        d[i] = oo;
    d[S] = 0;
    pq.push(S);
    InQueue[S] = 1;
    while(!pq.empty()){
        int nodCurent = pq.top();
        InQueue[nodCurent] = 0;
        pq.pop();
        for(node *p = L[nodCurent]; p != nullptr; p = p->next)
            if(d[nodCurent] + p->c < d[p->v])
            {
                d[p->v] = d[nodCurent] + p->c;
                if(InQueue[p->v] == 0){
                    InQueue[p->v] = 1;
                    pq.push(p->v);
                }
            }
    }
    if(check(N,d,db) == 1) g<<"DA"<<"\n";
    else g<<"NU"<<"\n";
}

int main(){
    f>>T;
    while(T){
        node *L[DIM];
        citire(N,M,S,db,L);
        Dijkstra_Heap(N,S,db,L);
        T--;
    }
}