Pagini recente » Cod sursa (job #2657765) | Cod sursa (job #1541402) | Cod sursa (job #1404806) | Cod sursa (job #518452) | Cod sursa (job #3269816)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("distante.in");
ofstream fout("distante.out");
const int NMAX=50000;
const int INF=INT_MAX;
int t, n, m, s, x, y, c, ok;
vector <pair <int,int>> a[NMAX];
vector <int> distin(NMAX,INF), dist(NMAX,INF);
priority_queue <pair <int,int>> heap;
bitset <NMAX> viz;
int main(){
fin>>t;
for(int k=1;k<=t;k++){
fin>>n>>m>>s;
for(int i=1;i<=n;i++)
fin>>distin[i];
for(int i=1;i<=m;i++){
fin>>x>>y>>c;
a[x].push_back({y,c});
a[y].push_back({x,c});
}
dist[s]=0;
heap.push({0,s});
while(!heap.empty()){
int nod=heap.top().second;
int d=-heap.top().first;
heap.pop();
if(!viz[nod]){
for(pair <int,int> next:a[nod]){
if(dist[next.first]>d+next.second){
dist[next.first]=d+next.second;
heap.push({-dist[next.first],next.first});
}
}
viz[nod]=1;
}
}
ok=1;
for(int i=1;i<=n&&ok==1;i++){
if(distin[i]!=dist[i]) ok=0;
}
if(ok) fout<<"DA"<<endl;
else fout<<"NU"<<endl;
viz.reset();
distin.clear();
dist.clear();
dist.assign(NMAX,INF);
for(int i=1;i<=n;i++)
a[i].clear();
}
return 0;
}