Pagini recente » Cod sursa (job #2904039) | Cod sursa (job #2894531) | Cod sursa (job #1467533) | Cod sursa (job #2732938) | Cod sursa (job #2769856)
#include <bits/stdc++.h>
#define dim 100002
#define int long long
using namespace std;
ifstream fin ("distante.in");
ofstream fout("distante.out");
int w[dim],sol[dim];
const int inf=100000000000000;
struct el
{
int nod,cost;
bool operator < (const el &A) const
{
return cost>A.cost;
}
};
priority_queue<el>pq;
vector<el>a[dim];
void dijkstra()
{
while (!pq.empty())
{
el x=pq.top();
pq.pop();
if (x.cost==sol[x.nod])
for (auto y: a[x.nod])
if (x.cost+y.cost<sol[y.nod])
{
sol[y.nod]=x.cost+y.cost;
pq.push({y.nod,sol[y.nod]});
}
}
}
void Solve ()
{
int i,n,m,s,x,y,z;
fin>>n>>m>>s;
for (i=1; i<=n; i++)
{
fin>>w[i];
a[i].clear();
}
while (m--)
{
fin>>x>>y>>z;
a[y].push_back({x,z});
a[x].push_back({y,z});
}
for (i=1; i<=n; i++)
sol[i]=inf;
sol[s]=0;
pq.push({s,0});
dijkstra();
for (i=1; i<=n; i++)
if (sol[i]!=w[i])
{
fout<<"NU"<<'\n';
return;
}
fout<<"DA"<<'\n';
}
int32_t main()
{
int t;
fin>>t;
while (t--)
{
Solve();
}
return 0;
}