Cod sursa(job #720263)

Utilizator andunhillMacarescu Sebastian andunhill Data 22 martie 2012 15:19:39
Problema Distante Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.4 kb
#include<fstream>
#include<vector>
#include<queue>
using namespace std;

#define pii pair<int, int>

ifstream f("distante.in");
ofstream g("distante.out");

int T, N, M, S;
int dist_bronz[50001];
int dist[50001];

vector< pii >graph[50001];

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

priority_queue<int, vector<int>, cmp>q;
bool inq[50001];


void read()
{	int i, j, x, y, c;
	
	f>>N>>M>>S;
	for(i = 1; i <= N; i++)
		f>>dist_bronz[i];
	
	for(i = 1; i <= M; i++)
	{	f>>x>>y>>c;
		graph[x].push_back(make_pair(y, c));
		graph[y].push_back(make_pair(x, c));
	}
}

void clear()
{	for(int i = 1; i <= N; i++)
		graph[i].clear();
}

void solve()
{	int i, j;
	
	for(i = 1; i <= N; i++) dist[i] = 1<<30;
	dist[S] = 0;
	
	q.push(S);
	inq[S] = true;
	
	while(!q.empty())
	{	i = q.top(); q.pop();
		
		inq[i] = false;
		for(vector< pii >::iterator it = graph[i].begin(); it != graph[i].end(); it++)
		{	if(dist[it->first] > dist[i] + it->second)
			{	dist[it->first] = dist[i] + it->second;
				
				if(!inq[it->first])
					inq[it->first] = true, q.push(it->first);
			}
		}
	}
	for(i = 1; i <= N; i++)
		if(dist[i] != dist_bronz[i])
		{	g<<"NU\n";
			return;
		}
	g<<"DA\n";
}
	

int main()
{	int i, j;
	
	f>>T;
	for(i = 1; i <= T; i++)
	{	read();
		solve();
		clear();
	}
	
	f.close();
	g.close();
	return 0;
}