Cod sursa(job #891119)

Utilizator fhandreiAndrei Hareza fhandrei Data 25 februarie 2013 13:49:19
Problema Distante Scor 50
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.7 kb
// Include
#include <fstream>
#include <cstring>
#include <vector>
#include <utility>
#include <queue>
using namespace std;

// Definitii
#define pb push_back
#define mp make_pair

#define edge pair<int, int>
#define cost first
#define node second

// Constante
const int sz = (int)5e4+1;

const int memsetOO = (1<<6)-1;
const int oo = 1061109567;

// Functii
void dijkstra(int startNode);

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

int tests;
int nodes, edges, startNode;

int dist[sz], bronzanelDist[sz];

vector<edge> graph[sz];

// Main
int main()
{
	in >> tests;
	while(tests--)
	{
		in >> nodes >> edges >> startNode;
		for(int i=1 ; i<=nodes ; ++i)
			in >> bronzanelDist[i];
		
		for(int i=1 ; i<=nodes ; ++i)
			graph[i].clear();
		
		memset(dist, memsetOO, sizeof(dist));
		
		int rFrom, rTo, rCost;
		while(edges--)
		{
			in >> rFrom >> rTo >> rCost;
			graph[rFrom].pb(mp(rCost, rTo));
			graph[rTo].pb(mp(rCost, rFrom));
		}
		
		dijkstra(startNode);
		
		if(memcmp(dist+1, bronzanelDist+1, nodes*4))
			out << "NU" << '\n';
		else
			out << "DA" << '\n';
	}
	
	in.close();
	out.close();
	return 0;
}

void dijkstra(int startNode)
{
	priority_queue< edge, vector<edge>, greater<edge> > heap;
	
	dist[startNode] = 0;
	heap.push(mp(0, startNode));
	
	while(!heap.empty())
	{
		int current = heap.top().node;
		heap.pop();
		
		vector<edge>::iterator it, end=graph[current].end();
		for(it=graph[current].begin() ; it!=end ; ++it)
		{
			if(dist[current] + it->cost < dist[it->node])
			{
				dist[it->node] = dist[current] + it->cost;
				heap.push(mp(dist[it->node], it->node));
			}
		}
	}
}