Pagini recente » Cod sursa (job #1209121) | Cod sursa (job #2499217) | Cod sursa (job #2316280) | Cod sursa (job #2279414) | Cod sursa (job #1750857)
#include <iostream>
#include <cstdio>
#include <queue>
#define MAXN 50050
#define DIM 300000
#define inf 0x3f3f3f3f
using namespace std;
int t, n, m, s, cursor;
int d[MAXN];
char buf[DIM];
struct rel
{
int y, c;
rel(int y, int c) : y(y), c(c) { }
};
vector<rel> v[MAXN];
char getChar()
{
if (cursor == DIM) {
fread(buf, 1, DIM, stdin);
cursor = 0;
}
return buf[cursor];
}
int getInt()
{
for (char c = getChar(); c < '0' || c > '9'; c = getChar())
cursor++;
int nr = 0;
for (char c = getChar(); c >= '0' && c <= '9'; c = getChar()) {
nr = nr*10 + c - '0';
cursor++;
}
return nr;
}
void citire()
{
n = getInt(); m = getInt(); s = getInt();
for (int i = 1; i <= n; i++)
d[i] = getInt(), v[i].clear();
for (int i = 1; i <= m; i++) {
int x = getInt();
int y = getInt();
int c = getInt();
v[x].push_back(rel(y, c));
v[y].push_back(rel(x, c));
}
}
int inq[MAXN], best[MAXN];
bool solve()
{
queue<int> q;
for (int i = 1; i <= n; i++)
inq[i] = 0, best[i] = inf;
for (q.push(s), best[s] = 0; !q.empty(); q.pop()) {
int top = q.front();
inq[top] = 0;
for (rel r : v[top]) {
if (best[top] + r.c < best[r.y]) {
best[r.y] = best[top] + r.c;
if (!inq[r.y]) {
q.push(r.y);
inq[r.y] = 1;
}
}
}
}
for (int i = 1; i <= n; i++)
if (best[i] != d[i])
return 0;
return 1;
}
int main()
{
freopen("distante.in", "r", stdin);
freopen("distante.out", "w", stdout);
fread(buf, 1, DIM, stdin);
t = getInt();
while (t--) {
citire();
int rez = solve();
if (rez == 1)
printf("DA\n");
else
printf("NU\n");
}
return 0;
}