Pagini recente » Cod sursa (job #3145637) | Cod sursa (job #2351241) | Cod sursa (job #861863) | Cod sursa (job #3291548) | Cod sursa (job #3285993)
#include <iostream>
#include <fstream>
#include <queue>
#define PII pair<int,int>
#define inf 1e9
using namespace std;
ifstream in ("distante.in");
ofstream out("distante.out");
priority_queue<PII,vector <PII>, greater<PII> >h;
int n,m,start,t;
int d[50005],sol[50005];
void djk(int strt,vector<PII> v[])
{
for(int i=1;i<=n;i++)
d[i]=inf;
d[strt]=0;
h.push({0,strt});
while(!h.empty())
{
int s=h.top().second;
int dist=h.top().first;
h.pop();
if(d[s]!=dist) continue;
for(auto it : v[s])
{
int nod=it.second;
int dc=it.first;
if(d[nod]>dist+dc)
{
d[nod]=dist+dc;
h.push({d[nod],nod});
}
}
}
}
int main()
{
in>>t;
for(int iii=1;iii<=t;iii++)
{ vector <PII> v[50005];
in>>n>>m>>start;
for(int i=1;i<=n;i++)
in>>sol[i];
for(int i=1;i<=m;i++)
{
int x,y,c;
in>>x>>y>>c;
v[x].push_back({c,y});
v[y].push_back({c,x});
}
djk(start,v);
bool ok=1;
for(int i=1;i<=n;i++)
if(d[i]!=sol[i])
ok=0;
if(ok) out<<"DA"<<'\n';
else out<<"NU"<<'\n';
}
return 0;
}