Pagini recente » Cod sursa (job #1194558) | Cod sursa (job #2989949) | Cod sursa (job #1383353) | Cod sursa (job #2989945) | Cod sursa (job #2569488)
#include <bits/stdc++.h>
#define mp make_pair
#define f first
#define s second
#define ll unsigned long long
#define pi pair <long long , long long>
using namespace std;
ifstream fin("distante.in");
ofstream fout("distante.out");
ll t, n, m, s, d[50005], ans[50005];
bool viz[50005];
vector < pair <ll, ll> > v[50005];
priority_queue < pi, vector <pi>, greater<pi> > pq;
void Dijkstra(int start)
{
pq.push(mp(0, start));
while(!pq.empty())
{
ll dist = pq.top().f;
ll nod = pq.top().s;
pq.pop();
if(viz[nod]) continue;
viz[nod] = true;
ans[nod] = dist;
for(int i = 0; i < v[nod].size(); i++)
{
int nxt, cost;
nxt = v[nod][i].f;
cost = v[nod][i].s;
if(!viz[nxt])
pq.push(mp(dist+cost, nxt));
}
}
}
int main()
{
fin >> t;
while(t--)
{
fin >> n >> m >> s;
for(int i = 1; i <= n; i++)
{
fin >> d[i];
viz[i] = 0;
}
ans[s] = 0;
for(int i = 1; i <= m; i++)
{
int a, b, c;
fin >> a >> b >> c;
v[a].push_back(mp(b, c));
v[b].push_back(mp(a, c));
}
Dijkstra(s);
bool ok = true;
for(int i = 1; i <= n; i++)
if(d[i] != ans[i])
{
ok = false;
break;
}
if(ok)
fout << "DA" <<'\n';
else
fout << "NU" << '\n';
for(int i = 1; i <= n; i++)
v[i].clear();
}
return 0;
}