Cod sursa(job #372283)

Utilizator cristiprgPrigoana Cristian cristiprg Data 9 decembrie 2009 09:36:49
Problema Distante Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.38 kb
#include <cstdio>
#define DIM 50005


int n, m, s, d[DIM];

struct nod
{
	int x, cost;
	nod *next;
};

nod *v[DIM];

void afisare()
{
	printf ("||----------||\n");
	for (int i = 1; i <= n; ++i)
	{
		nod *t = v[i];
		printf ("%d: ", i);
		while (t != NULL)
		{
			printf("%d ", t->x);
			t = t -> next;
		}
		printf ("\n");
	}
}

int solve()
{
	if (d[s] != 0)
		return 0;
	
	
	for (int y = 1, gasit = 0; y <= n; ++y, gasit = 0)
	{
		if (y == s)
			continue;
		nod *t = v[y];
//		printf("y = %d\n", y);
		while (t != NULL)
		{
//			printf ("d[%d] = d[%d] + %d\n", y, t->x, t->cost);
			if (d[y] == d[t -> x] + t -> cost)
			{
				gasit = 1;
				break;
			}
			
			t = t -> next;
		}
		
		if (!gasit)
			return 0;
	}
	
	return 1;
}

int main()
{
	FILE *f =fopen("distante.in", "r"), *g = fopen("distante.out", "w");
	
	int t;
	fscanf(f, "%d", &t);
	for (;t;--t)
	{
		fscanf(f, "%d%d%d", &n, &m, &s);
		for (int i = 1; i <= n; ++i)
			fscanf(f, "%d", &d[i]), v[i] = NULL;
		
		//graf
		int a, b, c;
		for (int i = 1; i <= m; ++i)
		{
			fscanf(f, "%d%d%d", &a, &b, &c);
			if (a == b)
				continue;
			// a[x][y] = 1
			nod *t = new nod;
			t -> x = b;
			t -> cost = c;
			t -> next = v[a];
			v[a] = t;
			
			t = new nod;
			t -> x = a;
			t -> cost = c;
			t -> next = v[b];
			v[b] = t;
			
			
			
		}
		
		fprintf(g, "%s\n", solve()==1?"DA":"NU");
	//	afisare();
		
	}
	
	fclose(f);
	fclose(g);
	return 0;
}