Cod sursa(job #286520)

Utilizator sigridMaria Stanciu sigrid Data 23 martie 2009 21:18:18
Problema Distante Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.52 kb
#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;
}