Pagini recente » Cod sursa (job #13159) | Cod sursa (job #1048805) | Cod sursa (job #104023) | Cod sursa (job #2597681) | Cod sursa (job #13477)
Cod sursa(job #13477)
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
const int nmax=50010;
const int mmax=100010;
int n,m,t,i,j,k,l,x,y,a[nmax],c[nmax],ok,b[4][mmax],d[nmax+mmax],cst[nmax+mmax];
int *vec[nmax],*cost[nmax];
int main()
{
freopen("distante.in","r",stdin);
freopen("distante.out","w",stdout);
scanf("%d",&t);
for (l=1;l<=t;++l)
{
scanf("%d %d %d",&n,&m,&x);
for (i=1;i<=n;++i) scanf("%d ",&c[i]);
for (i=1;i<=n;++i) a[i]=0;
for (i=1;i<=m;++i)
{
scanf("%d %d %d",&j,&k,&y);
a[j]++;
a[k]++;
b[1][i]=j;
b[2][i]=k;
b[3][i]=y;
}
for (i=1;i<=n;++i)
{
vec[i]=(int *)malloc(sizeof(int)*(a[i]+1));
cost[i]=(int *)malloc(sizeof(int)*(a[i]+1));
}
for (i=1;i<=m;++i) vec[i][0]=0;
for (i=1;i<=m;++i)
{
vec[b[1][i]][0]++;
vec[b[1][i]][vec[b[1][i]][0]]=b[2][i];
cost[b[1][i]][vec[b[1][i]][0]]=b[3][i];
vec[b[2][i]][0]++;
vec[b[2][i]][vec[b[2][i]][0]]=b[1][i];
cost[b[2][i]][vec[b[2][i]][0]]=b[3][i];
}
for (i=1;i<=n;++i)
{
a[i]=0; // a = vector de vizitati
cst[i]=mmax;
}
i=1;
j=1;
d[1]=x;
a[x]=1;
cst[1]=0;
if (c[x]==0) ok=1; else ok=0;
if (ok==1)
{
while (i<=j)
{
for (k=1;k<=vec[d[i]][0];++k)
{
if (a[vec[d[i]][k]]==0)
{
if (cst[d[i]]+cost[d[i]][k]<cst[vec[d[i]][k]])
{
j++;
d[j]=vec[d[i]][k];
a[d[j]]=1;
cst[d[j]]=cst[d[i]]+cost[d[i]][k];
}
}
else
{
if (cst[d[i]]+cost[d[i]][k]<cst[vec[d[i]][k]])
{
cst[vec[d[i]][k]]=cst[d[i]]+cost[d[i]][k];
}
}
}
a[d[i]]=0;
i++;
}
}
for (i=1;i<=n;++i)
if (c[i]!=cst[i])
{
ok=0;
break;
}
if (ok==1) printf("DA\n"); else printf("NU\n");
}
return 0;
}