Pagini recente » Cod sursa (job #829028) | Cod sursa (job #1540989) | Cod sursa (job #235168) | Cod sursa (job #1503280) | Cod sursa (job #1585161)
#include<cstdio>
#include<vector>
using namespace std;
FILE *in,*out;
struct edge
{
int t,l;
}here;
struct lst
{
vector<edge>v;
}adj[50001];
int edges,n,nr,start,heap[50001],sol[50001],dist[50001],L;
bool wrong;
const int inf=2000000000;
vector<edge>::iterator it;
void dijkstra();
void heap_push();
void heap_pop();
int main()
{
in=fopen("distante.in","r");
out=fopen("distante.out","w");
int i,j,x,a;
fscanf(in,"%d",&nr);
for(i=1;i<=nr;++i)
{
wrong=0;
fscanf(in,"%d%d%d",&n,&edges,&start);
for(j=1;j<=n;++j)fscanf(in,"%d",&sol[j]);
for(j=1;j<=edges;++j)
{
fscanf(in,"%d%d%d",&x,&here.t,&here.l);
adj[x].v.push_back(here);
a=here.l;
here.l=x;
x=a;
adj[x].v.push_back(here);
}
heap[++L]=start;
dijkstra();
if(wrong)fprintf(out,"NU\n");
else fprintf(out,"DA\n");
}
fclose(in);fclose(out);
return 0;
}
void dijkstra()
{
int i,x;
for(i=1;i<=n;++i)dist[i]=inf;
dist[start]=0;
if(sol[start])wrong=1;
while(L&&!wrong)
{
x=heap[1];
heap_pop();
for(it=adj[x].v.begin();it!=adj[x].v.end();++it)if(dist[(*it).t]>dist[x]+(*it).l)
{
dist[(*it).t]=dist[x]+(*it).l;
if(dist[(*it).t]!=sol[(*it).t])
{
wrong=1;
break;
}
heap[++L]=(*it).t;
heap_push();
}
}
while(L)
{
heap[L]=0;
--L;
}
for(i=1;i<=n;++i)adj[x].v.clear();
}
void heap_push()
{
int aux,ll=L;
while(ll/2&&dist[heap[ll/2]]>dist[heap[ll]])
{
aux=heap[ll];
heap[ll]=heap[ll/2];
heap[ll/2]=aux;
ll/=2;
}
}
void heap_pop()
{
heap[1]=heap[L];
heap[L--]=0;
int x=1,y=0,aux;
while(x!=y)
{
y=x;
if(y*2<=L&&dist[heap[x]]>dist[heap[2*y]])x=2*y;
if(y*2+1<=L&&dist[heap[x]]>dist[heap[2*y+1]])x=2*y+1;
aux=heap[x];
heap[x]=heap[y];
heap[y]=aux;
}
}