Pagini recente » Cod sursa (job #3156870) | Cod sursa (job #1806443) | Cod sursa (job #2449932) | Cod sursa (job #13328) | Cod sursa (job #286520)
Cod sursa(job #286520)
#include<stdio.h>
#define dim 50002
struct lista
{
int varf;
int cost;
lista *urm;
};
lista *l[dim], *ultim[dim], *aux;
int N, M, X0, T;
int d[dim];
void adauga(int x, int y, int c)
{
if( l[x] == 0 )
{
l[x] = new lista;
l[x] -> varf = y;
l[x] -> cost = c;
l[x] -> urm = 0;
ultim[x] = l[x];
}
else
{
aux = new lista;
aux -> varf = y;
aux -> cost = c;
aux -> urm = 0;
ultim[x] -> urm = aux;
}
}
int main()
{
int i, x, y, c, ok, ok2;
freopen("distante.in", "r", stdin);
freopen("distante.out", "w", stdout);
scanf("%d", &T);
while(T)
{
ok=1;
scanf("%d %d %d", &N, &M, &X0);
for(i = 1; i <= N; i++)
scanf("%d", &d[i]);
for(i = 1; i <= M; i++)
{
scanf("%d %d %d", &x, &y, &c);
adauga(x, y, c);
adauga(y, x, c);
if( ( d[x] + c ) < d[y] ) ok = 0;
}
if( d[X0] ) ok = 0;
if( ok )
{
for(i = 1; i <= N; i++)
if(i != X0)
{
ok2=0;
while( ( ! ok2 ) && ( l[i] -> urm ) )
{
if( ( d[ l[i] -> varf ] + l[i] -> cost ) == d[i])
{
ok2=1;
break;
}
l[i] = l[i] -> urm;
}
if( ! ok2 )
{
ok=0;
break;
}
}
}
if( ok ) printf("DA \n");
else printf("NU \n");
T--;
}
return 0;
}