Pagini recente » Cod sursa (job #950985) | Cod sursa (job #1591382) | Cod sursa (job #1085368) | Cod sursa (job #1834183) | Cod sursa (job #2456168)
#include <iostream>
#include <cstdio>
#include <vector>
#include <queue>
using namespace std;
const int N = 50000 + 7;
const int INF = (int) 1e9;
int t;
int n, m, s;
vector <pair <int, int>> g[N];
int dr[N];
int d[N];
bool ok() {
for (int i = 1; i <= n; i++) {
for (auto &it : g[i]) {
int j = it.first;
int c = it.second;
if (dr[i] + c < dr[j] || dr[j] + c < dr[i]) {
return 0;
}
}
}
priority_queue <pair <int, int>> pq;
pq.push({0, s});
while (!pq.empty()) {
int nod = pq.top().second;
int cur_dist = -pq.top().first;
pq.pop();
if (d[nod] != cur_dist) {
continue;
}
for (auto &it : g[nod]) {
int nou = it.first;
int nw_dist = cur_dist + it.second;
if (nw_dist < d[nou]) {
d[nou] = nw_dist;
pq.push({-nw_dist, nou});
}
}
}
for (int i = 1; i <= n; i++) {
if (d[i] != dr[i]) {
return 0;
}
}
return 1;
}
int main() {
freopen ("distante.in", "r", stdin);
freopen ("distante.out", "w", stdout);
scanf("%d", &t);
for (int tc = 1; tc <= t; tc++) {
scanf("%d %d %d", &n, &m, &s);
for (int i = 1; i <= n; i++) {
scanf("%d", &dr[i]);
g[i].clear();
d[i] = INF;
}
d[s] = 0;
for (int i = 1; i <= m; i++) {
int a, b, c;
scanf("%d %d %d", &a, &b, &c);
g[a].push_back({b, c});
g[b].push_back({a, c});
}
if (ok()) {
printf("DA\n");
} else {
printf("NU\n");
}
}
return 0;
}