Pagini recente » Cod sursa (job #563552) | Cod sursa (job #2933985) | Cod sursa (job #2584490) | Cod sursa (job #1497860) | Cod sursa (job #2916656)
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
#define pii pair <int,int>
ifstream in ("distante.in");
ofstream out ("distante.out");
const int max_size = 5e4 + 1, INF = 2e9 + 1;
int d[max_size], ans[max_size];
vector <pii> edges[max_size];
queue <int> coada;
void bfs (int nod)
{
d[nod] = 0;
coada.push(nod);
while (!coada.empty())
{
nod = coada.front();
coada.pop();
for (auto f : edges[nod])
{
if (d[f.first] > d[nod] + f.second)
{
d[f.first] = d[nod] + f.second;
coada.push(f.first);
}
}
}
}
void solve ()
{
int n, q, a;
in >> n >> q >> a;
for (int i = 1; i <= n; i++)
{
in >> ans[i];
d[i] = INF;
edges[i].clear();
}
while (q--)
{
int x, y, z;
in >> x >> y >> z;
edges[x].push_back({y, z});
edges[y].push_back({x, z});
}
bfs(a);
bool flag = true;
for (int i = 1; i <= n && flag; i++)
{
if (d[i] != ans[i])
{
flag = false;
}
}
if (flag)
{
out << "DA";
}
else
{
out << "NU";
}
out << '\n';
}
int main ()
{
int t;
in >> t;
while (t--)
{
solve();
}
in.close();
out.close();
return 0;
}