Pagini recente » Cod sursa (job #528180) | Cod sursa (job #2501723) | Cod sursa (job #1364723) | Cod sursa (job #3234027) | Cod sursa (job #3237525)
#include <fstream>
#include <queue>
#include <vector>
#define inf 1e9
#define nmax 50001
using namespace std;
ifstream cin("distante.in");
ofstream cout("distante.out");
int n,s,m,x,y,c,t,d[nmax],sol[nmax];
bool viz[nmax];
struct muchie{
int x,c;
};
vector<muchie>v[nmax];
struct cmp{
bool operator()(const muchie& a,const muchie& b){
return a.c>b.c;
}
};
priority_queue<muchie,vector<muchie>,cmp>q;
void initializare(){
for(int i=1;i<=n;i++){
v[i].clear();
d[i]=inf;
viz[i]=0;
}
}
void dijkstra(){
d[s]=0;
q.push({s,0});
while(!q.empty()){
int x=q.top().x;
q.pop();
if(viz[x])
continue;
viz[x]=1;
for(auto i:v[x])
if(d[i.x]>d[x]+i.c){
d[i.x]=d[x]+i.c;
q.push({i.x,d[i.x]});
}
}
}
bool verif(){
for(int i=1;i<=n;i++)
if(d[i]!=sol[i])
return 0;
return 1;
}
int main()
{
cin>>t;
while(t--){
cin>>n>>m>>s;
for(int i=1;i<=n;i++)
cin>>sol[i];
initializare();
for(int i=1;i<=m;i++){
cin>>x>>y>>c;
v[x].push_back({y,c});
v[y].push_back({x,c});
}
dijkstra();
if(verif())
cout<<"DA";
else
cout<<"NU";
cout<<'\n';
}
return 0;
}