Pagini recente » Cod sursa (job #1176051) | Cod sursa (job #2307890) | Cod sursa (job #2358032) | Cod sursa (job #1772646) | Cod sursa (job #337044)
Cod sursa(job #337044)
#include <cstdio>
#define MaxN 50009
#define Inf 2000001
using namespace std;
struct nod
{
int x,c;
nod *urm;
};
nod *G[MaxN];
int n,m,k,S,heap[MaxN],cost[MaxN],poz[MaxN],verif[MaxN],t,i,j,x,y,c,sw;
void add(int x,int y,int c)
{
nod *aux=new nod;
aux->x=y;
aux->c=c;
aux->urm=G[x];
G[x]=aux;
}
void swap(int x,int y)
{
int aux=heap[x];
heap[x]=heap[y];
heap[y]=aux;
}
void urca_heap(int nod)
{
int tata;
while(nod>1)
{
tata=nod/2;
if(cost[heap[tata]]>cost[heap[nod]])
{
poz[heap[nod]]=tata;
poz[heap[tata]]=nod;
swap(nod,tata);
nod=tata;
}
else
nod=1;
}
}
void coboara_heap(int nod)
{
int fiu;
while(nod<=k)
{
fiu=nod;
if(2*nod<=k)
{
fiu=2*nod;
if(fiu+1<=k)
if(cost[heap[fiu]]>cost[heap[fiu+1]])
fiu++;
}
else
return;
if(cost[heap[fiu]]<cost[heap[nod]])
{
poz[heap[nod]]=fiu;
poz[heap[fiu]]=nod;
swap(nod,fiu);
nod=fiu;
}
else
return;
}
}
void sol()
{
int i,min;
for(i=1;i<=n;i++)
if(i!=S)
{
cost[i]=Inf; poz[i]=-1;
}
nod *aux;
poz[S]=1;
heap[++k]=S;
while(k)
{
min=heap[1];
swap(1,k);
poz[heap[1]]=1;
k--;
coboara_heap(1);
aux=G[min];
while(aux)
{
if(cost[aux->x]>cost[min]+aux->c)
{
cost[aux->x]=cost[min]+aux->c;
if(poz[aux->x]!=-1)
urca_heap(poz[aux->x]);
else
{
heap[++k]=aux->x;
poz[aux->x]=k;
urca_heap(poz[aux->x]);
}
}
aux=aux->urm;
}
}
}
int main()
{
freopen("distante.in","r",stdin);
freopen("distante.out","w",stdout);
scanf("%d",&t);
for(i=1;i<=t;i++)
{
scanf("%d%d%d",&n,&m,&S);
for(j=1;j<=n;j++)
{
scanf("%d",&verif[j]);
if(verif[j]==0 && j!=S)
verif[j]=Inf;
}
for(j=1;j<=m;j++)
{
scanf("%d%d%d",&x,&y,&c);
add(x,y,c);
add(y,x,c);
}
sol();
sw=1;
for(j=1;j<=n && sw;j++)
if(verif[j]!=cost[j])
sw=0;
if(sw)
printf("%s\n","DA");
else
printf("%s\n","NU");
}
return 0;
}