Pagini recente » Cod sursa (job #2968871) | Cod sursa (job #2945424) | Cod sursa (job #1809896) | Cod sursa (job #1438802) | Cod sursa (job #3221435)
#include <bits/stdc++.h>
#ifdef LOCAL
#define in cin
#define out cout
#endif
using namespace std;
#ifndef LOCAL
ifstream in("distante.in");
ofstream out("distante.out");
#endif
const int nmax = 5e4 + 5;
const int inf = 1e9;
struct state
{
int a, b;
bool operator<(const state &other) const
{
return b > other.b;
}
};
int n, m;
priority_queue<state> pq;
vector<pair<int, int>> adj[nmax];
bool viz[nmax];
int D[nmax];
int d[nmax];
bool dodij(int start)
{
d[start] = 0;
pq.push({start, d[start]});
while (!pq.empty())
{
auto nod = pq.top();
pq.pop();
if (viz[nod.a])
continue;
viz[nod.a] = 1;
if (d[nod.a] != D[nod.a])
{
return 0;
}
for (auto i : adj[nod.a])
{
if (d[i.first] > d[nod.a] + i.second)
{
d[i.first] = d[nod.a] + i.second;
pq.push({i.first, d[i.first]});
}
}
}
for (int i = 1; i <= n; i++)
{
if (d[i] != D[i])
return 0;
}
return 1;
}
void solve()
{
int s;
in >> n >> m >> s;
while (!pq.empty())
pq.pop();
for (int i = 1; i <= n; i++)
{
in >> D[i];
d[i] = inf;
viz[i] = 0;
adj[i].clear();
}
for (int i = 0; i < m; i++)
{
int a, b, c;
in >> a >> b >> c;
adj[a].push_back({b, c});
adj[b].push_back({a, c});
}
if (dodij(s))
{
out << "DA\n";
}
else
{
out << "NU\n";
}
}
int main()
{
int t;
in >> t;
while (t--)
{
solve();
}
}