Pagini recente » Cod sursa (job #2099354) | Cod sursa (job #116312) | Cod sursa (job #899460) | Cod sursa (job #1583129) | Cod sursa (job #82485)
Cod sursa(job #82485)
using namespace std;
#include <cstdio>
#include <algorithm>
#include <list>
#define oo 0x3f3f3f3f
#define pb push_back
#define maxn 50001
int d[maxn], d1[maxn];
struct nod { int n, c;nod(){}; nod(int a, int b){n=a;c=b;};};
list<nod>a[maxn];
int n;
int H[maxn*3], nh;
struct cmp{
bool operator()(const int &a, const int &b)const
{
if(d[a]>d[b]) return 1;
return 0;
}
};
void dijkstra(int s)
{
memset(d, oo, sizeof(d));
d[s]=0;
H[nh++]=s;
int u;
list<nod>::iterator i,last;
while(nh)
{
u=H[0];
pop_heap(H, H+nh, cmp());
--nh;
for(i=a[u].begin(), last=a[u].end(); i!=last ;++i)
if(d[u]+i->c<d[i->n])
{
d[i->n]=d[u]+i->c;
H[++nh]=i->n;
push_heap(H, H+nh-1, cmp());
}
}
}
int main()
{
freopen("distante.in","r",stdin);
freopen("distante.out","w",stdout);
int T,m, s, p, q, c;
scanf("%d\n", &T);
while(T--)
{
scanf("%d %d %d\n", &n, &m,&s);
for(int i=1;i<=n;++i) scanf("%d ", &d1[i]);
while(m--)
{
scanf("%d %d %d\n", &p, &q, &c);
a[p].pb(nod(q, c));
a[q].pb(nod(p, c));
}
dijkstra(s);
int ok=1;
for(int i=1;i<=n;++i) if(d[i]!=d1[i]) { ok=0;break;}
if(ok)printf("DA\n");
else printf("NU\n");
}
return 0;
}