Cod sursa(job #755516)

Utilizator ioanabIoana Bica ioanab Data 5 iunie 2012 23:18:39
Problema Distante Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.4 kb
#include <fstream>
#include <queue>
#include <vector>
#include <cstring>
using namespace std;

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

const int N=50005;
const int inf=0x3f3f3f3f;

int dist[N];
int dst[N];
bool use[N];

struct nod
{
	int x,cost;
	nod(int _x, int _cost)
	{
		x=_x;
		cost=_cost;
	}
	nod()
	{
		x=0;
		cost=0;
	}
	bool operator< (const nod &X) const
	{
		return cost>X.cost;
	}
};

vector <nod> v[N];

void dijkstra(int S)
{
	memset(use, false, sizeof(use));
	memset(dist, inf, sizeof(dist));
	int x,y,c;
	
	priority_queue <nod> h;
	
	dist[S]=0;
	
	h.push(nod(S,dist[S]));
	
	while(!h.empty())
	{
		x=h.top().x;
		h.pop();
		if(use[x])
			continue;
		use[x]=true;
		
		for(vector <nod> :: iterator it= v[x].begin(); it!=v[x].end();it++)
		{
			y=it->x;
			c=it->cost+dist[x];
			if(dist[y]>c)
			{
				dist[y]=c;
				h.push(nod(y,c));
			}
		}
	}
}

int main()
{
	int n,m,t,k,i,ok,s,x,y,c;
	
	in>>t;
	for(k=1;k<=t;k++)
	{
		in>>n>>m>>s;
		
		for(i=1;i<=n;i++)
			in>>dst[i];
		
		
		while(m--)
		{
			in>>x>>y>>c;
			v[x].push_back(nod(y,c));
			v[y].push_back(nod(x,c));
		}
		dijkstra(s);
		
		ok=1;
		for(i=1;i<=n;i++)
			if(dist[i]!=dst[i])
			{
				out<<"NU"<<"\n";
				ok=0;
				break;
			}
			
		if(ok)
			out<<"DA"<<"\n";
		
		for(i=1;i<=n;i++)
			v[i].clear();
	}
	
	return 0;
}