Cod sursa(job #1348598)

Utilizator hohoho97Valentin Nita hohoho97 Data 19 februarie 2015 19:40:22
Problema Amenzi Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.7 kb
#include <fstream>

#include <vector>
#include <set>

using namespace std;

class Edge
{
private:
	unsigned i, j;
	double w;

public:
	Edge();
	Edge(unsigned, unsigned, double = 0.0);
	unsigned one() const;
	unsigned other(unsigned) const;
	double weight() const;
	bool operator< (const Edge&) const;
};

Edge::Edge()
{
	this->i = 0;
	this->j = 0;
	this->w = 0;
}

Edge::Edge(unsigned start, unsigned end, double weight)
{
	this->i = start;
	this->j = end;
	this->w = weight;
}

unsigned Edge::one() const
{
	return this->i;
}

unsigned Edge::other(unsigned one) const
{
	if (one == this->i)
		return this->j;
	else if (one == this->j)
		return this->i;
	else
		return -1;
}

double Edge::weight() const
{
	return this->w;
}

bool Edge::operator< (const Edge& another) const
{
	return this->w < another.weight();
}

class WGraph
{
private:
	vector<multiset<Edge>> vertices;

public:
	WGraph(unsigned = 1024);
	unsigned size() const;
	void edge(unsigned, unsigned, double);
	const multiset<Edge>& adj(unsigned) const;
	unsigned degree(unsigned) const;
};

WGraph::WGraph(unsigned vertices)
{
	this->vertices.resize(vertices + 1);
}

unsigned WGraph::size() const
{
	return this->vertices.size();
}

void WGraph::edge(unsigned v1, unsigned v2, double w)
{
	this->vertices.at(v1).insert(Edge(v1, v2, w));
	this->vertices.at(v2).insert(Edge(v2, v1, w));
}

const multiset<Edge>& WGraph::adj(unsigned vertex) const
{
	return this->vertices.at(vertex);
}

unsigned WGraph::degree(unsigned vertex) const
{
	return this->vertices.at(vertex).size();
}

class Amenzi
{
public:
	Amenzi()
	{
		ifstream in("amenzi.in");

		in >> n >> m >> k >> p;

		g = new WGraph(n);

		int a, b, c;
		for (int i = 0; i < m; i++)
		{
			in >> a >> b >> c;
			g->edge(a, b, c);
		}

		vector<vector<int>> amenzi(3501, vector<int>(n + 1, 0));

		for (int i = 0; i < k; i++)
		{
			in >> a >> b >> c;
			amenzi[b][a] = +c;
		}

		vector<vector<int>> d(3501, vector<int>(n + 1, -1));
		d[0][1] = amenzi[0][1];

		for (int i = 1; i <= 3500; i++)
		{
			for (int j = 1; j <= n; j++)
			{
				if (d[i - 1][j] >= 0)
				{
					d[i][j] = max(d[i][j], d[i - 1][j] + amenzi[i][j]);
				}
				for (const auto& adj : g->adj(j))
				{
					if (adj.weight() <= i && d[i - adj.weight()][adj.other(j)] >= 0)
					{
						d[i][j] = max(d[i][j], d[i - adj.weight()][adj.other(j)] + amenzi[i][j]);
					}
				}
			}
		}

		ofstream out("amenzi.out");

		for (int i = 0; i < p; i++)
		{
			in >> a >> b;
			out << d[b][a] << '\n';
		}

		in.close();
		out.close();
	}

private:
	int n, m, k, p;
	WGraph* g;
};

int main()
{
	Amenzi();

	return 0;
}