Pagini recente » Clasament boolanelvsbobulous | Cod sursa (job #433701) | Cod sursa (job #1486747) | Cod sursa (job #322401) | Cod sursa (job #2456162)
#include <iostream>
#include <cstdio>
#include <vector>
#include <queue>
using namespace std;
const int N = 50000 + 7;
int n, m, s, d1[N], d[N];
vector <pair <int, int>> g[N];
bool ok;
void dij() {
priority_queue <pair <int, int>> pq;
pq.push({0, s});
while (ok && !pq.empty()) {
int dist = -pq.top().first;
int nod = pq.top().second;
pq.pop();
if (dist != d[nod]) {
continue;
}
for (auto &it : g[nod]) {
int nou = it.first;
int cost = it.second;
if (dist + cost < d[nou]) {
d[nou] = dist + cost;
pq.push({-d[nou], nou});
}
}
}
}
int main() {
freopen ("distante.in", "r", stdin);
freopen ("distante.out", "w", stdout);
int t;
cin >> t;
for (int tc = 1; tc <= t; tc++) {
ok = 1;
cin >> n >> m >> s;
for (int i = 1; i <= n; i++) {
cin >> d1[i];
d[i] = (1 << 30);
g[i].clear();
}
d[s] = 0;
for (int i = 1; i <= n; i++) {
int a, b, c;
cin >> a >> b >> c;
g[a].push_back({b, c});
g[b].push_back({a, c});
}
dij();
for (int i = 1; i <= n && ok; i++) {
if (d[i] != d1[i]) {
ok = 0;
}
}
if (ok) {
cout << "DA\n";
} else {
cout << "NU\n";
}
}
return 0;
}