Cod sursa(job #143748)

Utilizator gabitzish1Gabriel Bitis gabitzish1 Data 26 februarie 2008 20:29:17
Problema Distante Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.03 kb
#include <stdio.h>
#include <string.h>

int n, m, s, t, viz[50005], d[50005];

typedef struct nod
{
	int x, c;
	nod *a;
} *pNod;
pNod v[50005];

void citire()
{
	int i, x, y, c;
	pNod p;
	scanf("%d %d %d",&n,&m,&s);
	for (i = 1; i <= n; i++) scanf("%d",&d[i]);
	for (i = 1; i <= m; i++)
	{
		scanf("%d %d %d",&x,&y,&c);
		p = new nod;
		p -> x = y;
		p -> c = c;
		p -> a = v[x];
		v[x] = p;
	}
}

int main()
{
	freopen("distante.in","r",stdin);
	freopen("distante.out","w",stdout);

	scanf("%d",&t);
	int i, ok;
	pNod p;
	while (t--)
	{
		citire();
		memset(viz,0,sizeof(viz));
		ok = 1;
		if (d[s]) ok = 0;
		else
		{
			for (i = 1; i <= n; i++)
			{
				for (p = v[i]; p != NULL; p = p -> a)
				{
					if (d[i] + p->c < d[p->x]){ ok = 0; break;}
					else if (d[i] + p->c == d[p->x]) viz[p->x] = 1;
				}
				if (!ok) break;
			}
			if (ok) for (i = 1; i <= n; i++) if (!viz[i] && i != s) {ok = 0; break;}
		}

		if (ok) printf("DA\n");
		else printf("NU\n");
	}
	return 0;
}