Cod sursa(job #263969)

Utilizator ProtomanAndrei Purice Protoman Data 20 februarie 2009 23:51:15
Problema Amenzi Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.68 kb
#include <algorithm>
#include <stdio.h>
#include <vector>

#define pb push_back
#define timpMax 3510
#define MAX 156

using namespace std;

class drum
{
	public:
		int dest, cost;

		drum(int dest, int cost)
		{
			this->dest = dest, this->cost = cost;
		}
};

int verts, edges, incid, meets;
vector <drum> listDrum[MAX];
int matBonus[timpMax][MAX], matCastig[timpMax][MAX];

int main()
{
	freopen("amenzi.in", "r", stdin);
	freopen("amenzi.out", "w", stdout);

	scanf("%d %d %d %d", &verts, &edges, &incid, &meets);

	for (; edges; edges--)
	{
		int t, f, c;
		scanf("%d %d %d", &t, &f, &c);

		listDrum[t].pb(drum(f, c));
		listDrum[f].pb(drum(t, c));
	}

	for (; incid; incid--)
	{
		int nod, time, bonus;
		scanf("%d %d %d", &nod, &time, &bonus);

		matBonus[time][nod] += bonus;
	}

	// init
	for (int timp = 0; timp < timpMax; timp++)
		for (int nod = 1; nod <= verts; nod++)
			matCastig[timp][nod] = -1;

	matCastig[0][1] = 0;
	for (int timp = 0; timp < timpMax; timp++)
		for (int nod = 1; nod <= verts; nod++)
		{
			if (matCastig[timp][nod] != -1)
				matCastig[timp][nod] += matBonus[timp][nod];

			for (int i = 0; i < listDrum[nod].size(); i++)
				if (timp + listDrum[nod][i].cost < timpMax)
					matCastig[timp + listDrum[nod][i].cost][listDrum[nod][i].dest] = 
						max(matCastig[timp + listDrum[nod][i].cost][listDrum[nod][i].dest], matCastig[timp][nod]);

			matCastig[timp + 1][nod] = max(matCastig[timp + 1][nod], matCastig[timp][nod]);
		}

	for (; meets; meets--)
	{
		int Qnod, Qtime;
		scanf("%d %d", &Qnod, &Qtime);

		printf("%d\n", matCastig[Qtime][Qnod]);
	}

	fclose(stdin);
	fclose(stdout);
	return 0;
}