Cod sursa(job #419474)

Utilizator s_holmesSherlock Holmes s_holmes Data 17 martie 2010 15:56:18
Problema Distante Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.36 kb
#include <cstdio>
#include <vector>
#include <queue>
#define NMAX 50008
#define INF 1000000001
using namespace std;
int T;
int S;
int N ,M;
vector< pair<int,int> > v[NMAX];
int cost[NMAX];
int val[NMAX];

void citire()
{
	scanf("%d%d%d",&N, &M, &S);
	int x , y , c;
	for(int i = 1 ; i <= N ; i++)
		scanf("%d",&val[i]);
	for(int i = 1 ; i <= M ; i++)
	{
		scanf("%d %d %d",&x ,&y ,&c);
		v[x].push_back(make_pair(y, c));
		v[y].push_back(make_pair(x, c));
	}
}

struct cmp
{
	bool operator()(const int &a, const int &b)const
	{
		return cost[a] > cost[b];
	}
};

priority_queue<int , vector<int> ,cmp> Q;

void rezolva()
{
	vector<pair<int, int> >::iterator it;
	
	for(int i = 1 ; i <= N ; i++)
		if(i != S)
			cost[i] = INF;
	
	Q.push(S);
	while(!Q.empty())
	{
		int min = Q.top();
		Q.pop();
		for(it = v[min].begin() ; it != v[min].end() ; it++)
			if(cost[it->first] > cost[min] + it->second)
			{
				cost[it->first] = cost[min] + it -> second;
				Q.push(it->first);
			}
	}
	
}

void scrie()
{
	for(int i = 1 ; i <= N ; i++)
		if(cost[i] != val[i])
		{
			printf("NU\n");
			return;
		}
	printf("DA\n");
}

int main()
{
	freopen("distante.in","r",stdin);
	freopen("distante.out","w",stdout);
	scanf("%d",&T);
	for(int i = 1 ; i <= T ; i++)
	{
		citire();
		rezolva();
		scrie();
	}
	
	return 0;
}