Cod sursa(job #2306049)

Utilizator EdgeLordXDOvidiuPita EdgeLordXD Data 21 decembrie 2018 16:05:21
Problema Distante Scor 50
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.2 kb
#include <bits/stdc++.h>
#define N 100001
#define ll long long
#define f first
#define s second
using namespace std;
ifstream in("distante.in");
ofstream out("distante.out");
priority_queue<pair<int,int> > h;
vector<pair<int,int> > a[N];
int r[N/2], v[N/2];
bool dij(){
    int i,cur;
    while(!h.empty()){
        i=h.top().s;
        cur=-h.top().f;
        h.pop();
        if(v[i]!=-2){
            v[i]=-2;
            if(r[i]!=cur)
                return 0;
            for(auto it:a[i]){
                if(v[it.s]==-1 || cur+it.f<v[it.s]){
                    h.push({-(cur+it.f), it.s});
                    v[it.s]=cur+it.f;
                }
            }
        }
    }
    return 1;
}
int main(){
    int t,n,m,s,i,j,w,x,y;
    in>>t;
    while(t--){
        in>>n>>m>>s;
        for(i=1; i<=n; ++i)
            in>>r[i];
        for(i=1; i<=m; ++i){
            in>>x>>y>>w;
            a[x].push_back({w,y});
            a[y].push_back({w,x});
        }
        for(i=1; i<=n; ++i)
            v[i]=-1;
        h.push({0,s});
        out<<(dij()?"DA\n":"NU\n");
        memset(r,0,n+1);
        for(i=1; i<=n; ++i)
            a[i].clear();
    }
    return 0;
}