Pagini recente » Cod sursa (job #417848) | Cod sursa (job #1148192) | Cod sursa (job #1826274) | Cod sursa (job #1678833) | Cod sursa (job #2718417)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream in("distante.in");
ofstream out("distante.out");
const int INF = 1e9;
vector <pair <int, int>> a[50001];
queue <int> q;
int n,d[50001],v[50001];
bool inq[50001];
int main()
{
int m,t,s;
in>>t;
for(int contor=1;contor<=t;contor++){
in>>n>>m>>s;
for(int i=1;i<=n;i++)
in>>v[i];
for(int i=0;i<m;i++){
int x,y,c;
in>>x>>y>>c;
a[x].push_back({y,c});
a[y].push_back({x,c});
}
for(int i=1;i<=n;i++)
d[i]=INF;
q.push(s);
d[s]=0;
inq[s]=true;
while(!q.empty()){
int x=q.front();
q.pop();
inq[x]=false;
for (auto p:a[x]){
int y=p.first;
int c=p.second;
if(d[x]+c<d[y]){
d[y]=d[x]+c;
if(!inq[y]){
q.push(y);
inq[y]=true;
}
}
}
}
int ok=0;
for(int i=1;i<=n && ok==0;i++)
if(d[i]!=v[i])
ok=1;
if(ok==0)
out<<"DA"<<'\n';
else
out<<"NU"<<'\n';
for(int i=1;i<=n;i++){
a[i].clear();
inq[i]=0;
}
}
return 0;
}