Cod sursa(job #658757)

Utilizator alexdmotocMotoc Alexandru alexdmotoc Data 9 ianuarie 2012 14:58:09
Problema Distante Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.45 kb
#include <iostream>
#include <cstdio>
#include <vector>
#include <queue>

using namespace std;

#define PB push_back
#define MKP make_pair
#define f first
#define s second

#define maxM 100005
#define maxN 50005
#define INF 0x3f3f3f3f

vector <pair <int , int> > lista[maxM];
queue <int> Q;

int cost[maxN] , N , M , S , T , rez[maxN];

int solve ()
{
	int nod , aci , cst , cost_curent;
	
	for (int i = 1 ; i <= N ; ++i)
		cost[i] = INF;
	
	Q.push (S);
	cost[S] = 0;
	
	while (! Q.empty ())
	{
		nod = Q.front ();
		
		for (unsigned i = 0 ; i < lista[nod].size () ; ++i)
		{
			aci = lista[nod][i].f;
			cst = lista[nod][i].s;
			
			cost_curent = cost[nod] + cst;
			
			if (cost_curent < cost[aci])
			{
				cost[aci] = cost_curent;
				
				Q.push (aci);
			}
		}
		
		Q.pop ();
	}
	
	for (int i = 1 ; i <= N ; ++i)
		if (cost[i] != rez[i])
			return 0;
	
	return 1;
}



int main ()
{
	freopen ("distante.in" , "r" , stdin);
	freopen ("distante.out" , "w" , stdout);
	
	scanf ("%d" , &T);
	
	int a , b , c , sol;
	
	while (T --)
	{
		scanf ("%d %d %d" , &N , &M , &S);
		
		for (int i = 1 ; i <= N ; ++i)
			scanf ("%d" , &rez[i]);
		
		for (int i = 1 ; i <= M ; ++i)
		{
			scanf ("%d %d %d" , &a , &b , &c);
			
			lista[a].PB (MKP (b , c));
			lista[b].PB (MKP (a , c));
		}
		
		sol = solve ();
		
		if (sol == 1)
			printf ("DA\n");
		
		else printf ("NU\n");
	}
	
	return 0;
}