Pagini recente » Cod sursa (job #2079546) | Cod sursa (job #283085) | Cod sursa (job #543076) | Cod sursa (job #1296168) | Cod sursa (job #2306087)
#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");
for(i=1; i<=n; ++i)
a[i].clear();
}
return 0;
}