Pagini recente » Profil georgip | Statistici test9265 (test9265) | Rating chirila radu (chirilaradu1) | Istoria paginii utilizator/eumirea21 | Cod sursa (job #256952)
Cod sursa(job #256952)
#include<stdio.h>
#define max 500
long d[max], n, m, s, t;
struct elem
{ long v, c;
elem *urm;
} *a[max], *q;
FILE *f, *g;
void empty()
{ long i;
for(i=1; i<=n; i++)
a[i]=NULL;
}
void read()
{ long i, x, y, cs;
fscanf(f, "%ld%ld%ld", &n, &m, &s);
for(i=1; i<=n; i++)
fscanf(f, "%ld", &d[i]);
for(i=0; i<m; i++)
{ fscanf(f, "%ld%ld%ld", &x, &y, &cs);
q=new elem;
q->c=cs; q->v=y;
q->urm=a[x];
a[x]=q;
q=new elem;
q->c=cs; q->v=x;
q->urm=a[y];
a[y]=q;
}
/* for(i=1; i<=n; i++)
{ q=a[i];
while(q)
{cout<<q->v<<" ";
q=q->urm;
}
cout<<endl;
}
*/
}
int solve()
{ long i;
if(d[s]!=0)
return 0;
/* q=a[s];
while(q)
{ if(d[q->v]>q->c)
return 0;
q=q->urm;
}*/
for(i=1; i<=n; i++)
{ if(i!=s)
{ q=a[i];
while(q)
{ if(d[i]>d[q->v]+q->c)
return 0;
q=q->urm;
}
}
}
return 1;
}
int main()
{ int k, l;
f=fopen("distante.in", "r");
g=fopen("distante.out", "w");
fscanf(f, "%ld", &t);
for(l=0; l<t; l++)
{
read();
empty();
k=solve();
if(k==0)
fprintf(g, "%s\n", "NU");
else
fprintf(g, "%s\n", "DA");
}
fclose(g);
return 0;
}