Pagini recente » Cod sursa (job #305251) | Cod sursa (job #305105) | Cod sursa (job #633450) | Cod sursa (job #2665266) | Cod sursa (job #2665226)
#include <bits/stdc++.h>
#define INF INT_MAX
using namespace std;
const int Nmx = 10100;
int t, n, m, s,a ,b ,w, dist1[Nmx], dist[Nmx];
bool vis[Nmx], politai = true;
vector<pair<int,int>> adj[Nmx];
priority_queue<pair<int,int>> q;
int main(){
freopen("distante.in","r",stdin);
freopen("distante.out","w",stdout);
cin >> t;
while(t--){
cin >> n >> m >> s;
for(int i = 1; i <= n; i++) cin >> dist1[i];
for(int i = 1; i <= m; i++){
cin >> a >> b >> w;
adj[a].push_back({b,w});
adj[b].push_back({a,w});
}
for(int i = 1; i <= n; i++){ dist[i] = INF; vis[i] = false;}
dist[s] = 0;
q.push({0,s});
while(!q.empty()){
a = q.top().second; q.pop();
if(vis[a]) continue;
vis[a] = true;
for(auto u : adj[a]){
b = u.first; w = u.second;
if(dist[a]+w < dist[b]){
dist[b] = dist[a]+w;
q.push({-dist[b],b});
}
}
}
for(int i = 1; i <= n; i++) if(dist1[i] != dist[i]) politai = false;
if(politai) cout << "DA\n";
else cout << "NU\n";
}
}