Pagini recente » Profil Adela_Petre | Cod sursa (job #2646410) | Cod sursa (job #1106704) | Cod sursa (job #1420457) | Cod sursa (job #2594606)
#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=1;i<=n;i++){
if(d[i]==(1<<20) && 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;
while(!q.empty()){
q.pop();
}
for(int i=1;i<=n;i++){
v[i].clear();
}
}
return 0;
}