Pagini recente » Cod sursa (job #987474) | Cod sursa (job #1880934) | Cod sursa (job #817612) | Cod sursa (job #1258492) | Cod sursa (job #1243689)
#include <cstdio>
#include <cstring>
#include <vector>
#define INF 1000000
using namespace std;
int fiust (int x) {return x*2;}
int fiudr (int x) {return x*2+1;}
int tata (int x) {return x/2;}
struct distante
{
vector <int> nod;
vector <int> cost;
};
distante g[100001];
int h[100001], poz[100001], dist[100001], sol[100001], m;
void swap (int x, int y)
{
int aux=h[x];
h[x]=h[y];
h[y]=aux;
aux=poz[h[x]];
poz[h[x]]=poz[h[y]];
poz[h[y]]=aux;
}
void heapup (int x)
{
if (dist[h[x]]<dist[h[tata(x)]])
{
swap(x,tata(x));
heapup(tata(x));
}
}
void heapdown (int x)
{
int st, dr;
if (fiust(x)>m) return;
st=dist[h[fiust(x)]];
if (fiudr(x)<=m) dr=dist[h[fiudr(x)]];
else dr=st+1;
if (st<dr)
{
if (st<dist[h[x]])
{
swap(x,fiust(x));
heapdown(fiust(x));
}
}
else
{
if (dr<dist[h[x]])
{
swap(x,fiudr(x));
heapdown(fiudr(x));
}
}
}
int main()
{
int n, t, sursa, i, j, k, x, y, z, Nod;
freopen("distante.in","r",stdin);
freopen("distante.out","w",stdout);
scanf("%d",&t);
for (k=1; k<=t; k++)
{
memset(h,0,sizeof(h)); memset(poz,0,sizeof(poz)); memset(dist,0,sizeof(dist)); memset(sol,0,sizeof(sol));
scanf("%d%d%d",&n,&m,&sursa); bool ok=true;
for (i=1; i<=n; i++)
{
scanf("%d",&sol[i]); dist[i]=INF; h[i]=i; poz[i]=i;
while (!g[i].nod.empty())
{
g[i].nod.pop_back();
g[i].cost.pop_back();
}
} dist[sursa]=0;
for (i=1; i<=m; i++)
{
scanf("%d%d%d",&x,&y,&z);
g[x].nod.push_back(y);
g[y].nod.push_back(x);
g[x].cost.push_back(z);
g[y].cost.push_back(z);
} m=n;
for (i=1; i<=n; i++)
{
Nod=h[1];
swap(1,m); m--;
heapdown(1);
for (j=0; j<g[Nod].nod.size(); j++)
{
if (dist[Nod]+g[Nod].cost[j]<dist[g[Nod].nod[j]])
{
dist[g[Nod].nod[j]]=dist[Nod]+g[Nod].cost[j];
heapup(poz[g[Nod].nod[j]]);
}
}
}
for (i=1; i<=n; i++)
{
if (dist[i]==INF) dist[i]=0;
}
for (i=1; i<=n; i++)
{
if (sol[i]!=dist[i])
{
ok=false;
break;
}
}
if (ok==true) printf("DA\n");
else printf("NU\n");
}
fclose(stdin);
fclose(stdout);
return 0;
}