Pagini recente » Cod sursa (job #299446) | Cod sursa (job #1306654) | Cod sursa (job #1704825) | Cod sursa (job #720296) | Cod sursa (job #1858039)
#include <bits/stdc++.h>
#define MAXN 50010
using namespace std;
ifstream fin("distante.in");
ofstream fout("distante.out");
vector<pair<int, int> > v[MAXN];
set<pair<int, int> > h;
int n,m,s,T,x,y,w,ok;
int dist[MAXN];
int D[MAXN];
int main()
{
int top, nod, cost;
fin>>T;
while(T--)
{
for(int i = 0; i<=n; i++)
v[i].clear();
fin>>n>>m>>s;
for(int i=1; i<=n; i++)
fin>>D[i];
while(m--)
{
fin>>x>>y>>w;
v[x].push_back(make_pair(y,w));
v[y].push_back(make_pair(x,w));
}
for(int i=1; i<=n; i++)
dist[i] = MAXN;
dist[s] = 0;
h.insert(make_pair(0,s));
while(!h.empty())
{
top = h.begin()->second;
h.erase(h.begin());
for(auto it:v[top])
{
nod = it.first;
cost = it.second;
if(dist[nod] > dist[top] + cost)
{
if(dist[nod] != MAXN)
h.erase(h.find(make_pair(dist[nod], nod)));
dist[nod] = dist[top] + cost;
h.insert(make_pair(dist[nod], nod));
}
}
}
ok = 1;
for(int i=1; i<=n; i++)
{
if(dist[i] == MAXN)dist[i] = 0;
if(D[i] != dist[i])
{
ok=0;
break;
}
}
if(ok)
fout<<"DA\n";
else
fout<<"NU\n";
}
return 0;
}