Cod sursa(job #451579)

Utilizator andrei.sfrentSfrent Andrei andrei.sfrent Data 9 mai 2010 18:47:35
Problema Distante Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.75 kb
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
int d[50001], gasit[50001];
int main()
{
	freopen("distante.in", "r", stdin);
	freopen("distante.out", "w", stdout);
	int t, n, m, s, i, x, y, c, rez;
	scanf("%d", &t);
	while(t--)
	{
		scanf("%d%d%d", &n, &m, &s);
		for(i = 1; i <= n; ++i) scanf("%d", d + i), gasit[i] = 0;
		gasit[s] = rez = 1;
		if(d[s] != 0) rez = 0;
		for(i = 1; i <= m; ++i)
		{
			scanf("%d%d%d", &x, &y, &c);
			if(d[y] > d[x] + c) rez = 0;
			if(d[x] > d[y] + c) rez = 0;
			if(!gasit[y]) if(d[y] == d[x] + c) gasit[y] = 1;
			if(!gasit[x]) if(d[x] == d[y] + c) gasit[x] = 1;
		}
		for(i = 1; i <= n && rez == 1; ++i) if(!gasit[i]) rez = 0;
		if(rez) printf("DA\n");
		else printf("NU\n");
	}
	return 0;
}