Pagini recente » Cod sursa (job #2173082) | Cod sursa (job #426289) | Cod sursa (job #1082840) | Cod sursa (job #638908) | Cod sursa (job #3351020)
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <climits>
using namespace std;
const int MAXN = 50000 + 5;
const int INF = 1e9;
int main() {
freopen ("distante.in" , "r" , stdin);
freopen ("distante.out" , "w" , stdout);
int T;
cin >> T;
while (T--) {
int N, M, S;
cin >> N >> M >> S;
vector<int> D(N + 1);
for (int i = 1; i <= N; ++i) {
cin >> D[i];
}
if (D[S] != 0) {
cout << "NU\n";
for (int i = 0; i < M; ++i) {
int a, b, c;
cin >> a >> b >> c;
}
continue;
}
vector<vector<pair<int,int>>> G(N + 1);
bool valid = true;
for (int i = 0; i < M; ++i) {
int a, b, c;
cin >> a >> b >> c;
G[a].push_back({b, c});
G[b].push_back({a, c});
if (D[a] > D[b] + c || D[b] > D[a] + c) {
valid = false;
}
}
if (!valid) {
cout << "NU\n";
continue;
}
for (int v = 1; v <= N; ++v) {
if (v == S) continue;
if (D[v] == 0) {
valid = false;
continue;
}
bool can_be_reached = false;
for (auto &edge : G[v]) {
int u = edge.first, c = edge.second;
if (D[v] == D[u] + c) {
can_be_reached = true;
break;
}
if (D[u] + c < D[v]) {
valid = false;
}
}
if (!can_be_reached) {
valid = false;
}
}
vector<int> computed(N + 1, INF);
computed[S] = 0;
vector<bool> vis(N + 1, false);
vector<int> Q;
Q.push_back(S);
vis[S] = true;
for (int i = 0; i < (int)Q.size(); ++i) {
int u = Q[i];
for (auto &edge : G[u]) {
int v = edge.first, c = edge.second;
if (computed[u] + c < computed[v]) {
computed[v] = computed[u] + c;
if (!vis[v]) {
vis[v] = true;
Q.push_back(v);
}
}
}
}
for (int v = 1; v <= N; ++v) {
if (computed[v] < D[v]) {
valid = false;
}
}
cout << (valid ? "DA" : "NU") << "\n";
}
return 0;
}