Pagini recente » Cod sursa (job #3232561) | Cod sursa (job #876034) | Cod sursa (job #2902359) | Cod sursa (job #353393) | Cod sursa (job #4307)
Cod sursa(job #4307)
#include <cstdio>
#include <algorithm>
#include <string>
#include <queue>
#include <vector>
#define maxn 1<<16
#define oo 0x3f3f3f3f
using namespace std;
int H[maxn], n, m, T, d[maxn], d1[maxn];
struct nod{int nd, w; nod(int _nd, int _w){nd=_nd; w=_w;}; nod(){};};
vector<vector<nod> >a;
bool operator<(const nod &a, const nod &b)
{
if(a.w>b.w) return 1;
// if(a.w==b.w) if(a.nd<b.nd) return 1;
return 0;
}
void dijkstra(int s)
{
priority_queue<nod>Q;
bool viz[maxn];
int i;
nod u;
vector<nod>::iterator it;
memset(viz, 0, sizeof(viz));
memset(d, oo, sizeof(d));
Q.push(nod(s, 0));
d[s]=0;
int nh=n;
while(Q.size() || nh)
{
u=Q.top(); Q.pop();
if(viz[u.nd])continue;
if(d[u.nd]<u.w)continue;
viz[u.nd]=1;
nh--;
for(it=a[u.nd].begin();it!=a[u.nd].end();++it)
if(d[u.nd]+it->w < d[it->nd])
{
d[it->nd]=d[u.nd]+it->w;
Q.push(nod(it->nd, d[it->nd]));
}
}
}
int main()
{
freopen("distante.in", "r", stdin);
freopen("distante.out", "w", stdout);
int i, j,p, q, w, s;
scanf("%d", &T);
for(i=1;i<=T;i++)
{
a.clear();
scanf("%d %d %d\n", &n, &m, &s);
a.resize(n+1);
for(j=1;j<=n;j++) scanf("%d ", d1+j);
for(j=1;j<=m;j++)
{
scanf("%d %d %d\n", &p, &q, &w);
a[p].push_back(nod(q, w));
a[q].push_back(nod(p, w));
}
dijkstra(s);
int ok=1;
// for(j=1;j<=n;j++) printf("%d ", d[j]); printf("\n");
for(j=1;j<=n;j++) if(d1[j]!=d[j]){ ok=0; break;}
if(ok) printf("DA\n");
else printf("NU\n");
}
return 0;
}