Pagini recente » Cod sursa (job #3342232) | Cod sursa (job #1131707) | Cod sursa (job #3309546) | Cod sursa (job #2103947) | Cod sursa (job #3320455)
#include <bits/stdc++.h>
#define ll long long
#define INF 0x3f3f3f3f3f3f3f3f
using namespace std;
const int NMAX = 5e4;
struct node {
int nod;
ll cost;
};
bool operator < (const node & a, const node & b) {
return a.cost > b.cost;
}
void solve() {
int n, m, a, b, c, s;
cin >> n >> m >> s;
vector<int> initial(n + 1);
vector<ll> dist(n + 1, INF);
vector<vector<node>> v(n + 1);
for (int i = 1; i <= n; i++) cin >> initial[i];
while (m--) {
cin >> a >> b >> c;
v[a].push_back({b, c});
}
priority_queue<node> pq;
pq.push({s, 0});
while (pq.size()) {
while (pq.size() && dist[pq.top().nod] != INF) pq.pop();
if (pq.size()) {
node a = pq.top();
pq.pop();
dist[a.nod] = a.cost;
for (node i : v[a.nod]) {
if (dist[i.nod] == INF) {
pq.push({i.nod, i.cost + dist[a.nod]});
}
}
}
}
for (int i = 1; i <= n; i++) {
if (dist[i] != initial[i]) {
cout << "NU\n";
return;
}
}
cout << "DA\n";
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
#ifndef ONLINE_JUDGE
freopen("distante.in", "r", stdin);
freopen("distante.out", "w", stdout);
#endif
/*
freopen("D:\\cf2\\problema\\output.txt", "w", stdout);
freopen("D:\\cf2\\problema\\input.txt", "r", stdin);
*/
int tt; cin >> tt;
while (tt--) solve();
return 0;
}