Cod sursa(job #831517)

Utilizator moscaliuc_ovidiuMoscaliuc Ovidiu moscaliuc_ovidiu Data 8 decembrie 2012 18:26:40
Problema Radiatie Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.07 kb
#include <fstream>
#include <set>
#include <vector>
#include <stack>

#define MAX_N ((const unsigned int)15100)

class Edge
{
public:
	int x;
	int y;
	int cost;

public:
	Edge(int _x, int _y, int _cost) :
		x(_x), y(_y), cost(_cost)
	{
	}

	bool operator < (const Edge &_other) const
	{
		return this->cost < _other.cost;
	}
};

class Pair
{
public:
	int node;
	int cost;

public:
	Pair(int _node, int _cost) :
		node(_node), cost(_cost)
	{
	}

	bool operator < (const Pair &_other) const
	{
		return this->cost < _other.cost;
	}
};

typedef std::multiset<Edge>		Edges;
typedef std::set<int>			NodeSet;
typedef std::multiset<Pair>		Neighbours;

Neighbours Graf[MAX_N];
bool visited[MAX_N];
Edges edges;
NodeSet inserted;

int compute(int _current, int _dest, int _cost, bool &_bFound)
{
	if (_current == _dest)
	{
		_bFound = true;
		return _cost;
	}

	visited[_current] = true;

	Neighbours::iterator	itNeighbour;

	for (itNeighbour = Graf[_current].begin(); Graf[_current].end() != itNeighbour && !_bFound; ++ itNeighbour)
	{
		if (!visited[itNeighbour->node])
		{
			_cost = compute(itNeighbour->node, _dest, _cost > itNeighbour->cost ? _cost : itNeighbour->cost, _bFound);
		}
	}

	visited[_current] = false;

	return _cost;
}

int main()
{
	std::ifstream fin("radiatie.in");
	std::ofstream fout("radiatie.out");

	int N, M, K;
	int x, y;
	
	unsigned int c;

	fin>>N>>M>>K;

	for (int i = 0; i < M; ++ i)
	{
		fin>>x>>y>>c;

		edges.insert(Edge(x, y, c));
	}

	inserted.insert(1);

	while(inserted.size() != N)
	{
		Edges::iterator	itEdge;

		for (itEdge = edges.begin(); edges.end() != itEdge && inserted.find(itEdge->x) == inserted.end(); ++ itEdge);

		Edge edge = *itEdge;

		Graf[edge.x].insert(Pair(edge.y, edge.cost));
		Graf[edge.y].insert(Pair(edge.x, edge.cost));

		inserted.insert(itEdge->y);
		edges.erase(itEdge);
	}

	for (int i = 0; i < K; ++ i)
	{
		bool bFound = false;

		fin>>x>>y;
		fout<<compute(x, y, 0, bFound)<<'\n';
	}

	fin.close();
	fout.close();

	return 0;
}