Pagini recente » Cod sursa (job #2052763) | Cod sursa (job #2942933) | Cod sursa (job #2894817) | Cod sursa (job #695143) | Cod sursa (job #2449186)
#include <bits/stdc++.h>
#define pii pair<int, int>
///N=50000
///M=100000
///c=1000
///T=10
using namespace std;
int t, n, m, i, j, k, s;
int hypo[50001];
bool check[50001];
vector<pii> graph[50001];
queue<int> q;
///
int main()
{
freopen("distante.in", "r", stdin);
freopen("distante.out", "w", stdout);
scanf("%d", &t);
while(t--){
while(!q.empty()) q.pop();
for(i=1; i<=n; ++i){
graph[i].clear();
check[i]=false;
}
scanf("%d%d%d", &n, &m, &s);
for(i=1; i<=n; ++i) scanf("%d", &hypo[i]);
for(i=1; i<=m; ++i){
int x, y, c;
scanf("%d%d%d", &x, &y, &c);
graph[x].push_back({y, c});
graph[y].push_back({x, c});
}
if(!hypo[s]) {q.push(s); check[s]=true;}
while(!q.empty()){
int node=q.front(); q.pop();
for(auto next:graph[node]){
if(hypo[node]+next.second<hypo[next.first]) {check[node]=false; break;}
if(hypo[node]+next.second==hypo[next.first] && !check[next.first]){
check[next.first]=true;
q.push(next.first);
}
}
if(!check[node]) break;
}
for(i=1; i<=n; ++i) if(!check[i]){printf("NU\n"); break;}
if(i==n+1) printf("DA\n");
}
fclose(stdin);
fclose(stdout);
return 0;
}