Pagini recente » Cod sursa (job #1960705) | Cod sursa (job #530692) | Cod sursa (job #70010) | Cod sursa (job #84395) | Cod sursa (job #2594602)
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream cin("distante.in");
ofstream cout("distante.out");
priority_queue<pair <int,int>,vector <pair < int,int>>,greater<pair < int,int>>> q;
int t,n,m,x,y,z,d[50005],ok,d2[50005],nodulet;
vector<pair <int ,int>> v[250005];
int main()
{
cin>>t;
while(t--){
cin>>n>>m>>nodulet;
for(int i=1;i<=n;i++){
d[i]=(1<<20);
}
for(int i=1;i<=n;i++){
cin>>d2[i];
}
for(int i=1;i<=m;i++){
cin>>x>>y>>z;
v[x].push_back({y,z});
v[y].push_back({x,z});
}
d[nodulet]=0;
q.push({0,nodulet});
while(!q.empty()){
int nod=q.top().second;
int idk=q.top().first;
q.pop();
if(d[nod]!=idk){
continue;
}
for(auto x: v[nod]){
if(d[x.first]>d[nod]+x.second){
d[x.first]=d[nod]+x.second;
q.push({d[x.first],x.first});
}
}
}
for(int i=2;i<=n;i++){
if(d[i]==(1<<20) && d2[i]!=0 || (d[i]==0 && d2[i]!=0)){
ok=1;
}
else{
if(d[i]!=d2[i]){
ok=1;
}
}
}
if(ok==1){
cout<<"NU"<<'\n';
}
else
cout<<"DA"<<'\n';
ok=0;
}
return 0;
}