Cod sursa(job #920858)

Utilizator AndreiRSStatescu Andrei Rares AndreiRS Data 20 martie 2013 17:36:15
Problema Amenzi Scor 30
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.33 kb
#include <fstream>
#include <algorithm>
#include <vector>
#include <math.h>
#include <stdlib.h>
#include <string.h>
using namespace std;

ifstream fi ("amenzi.in");
ofstream fo ("amenzi.out");

const int nmax = 152, tmax = 3501;
int NN, NM, NA, NI, M[nmax][nmax], D[nmax][tmax];
vector <int> V[nmax];

void cit ()
{
	for (int n = 0, t; n < nmax; n++) for (t = 0; t < tmax; t++) D[n][t] = -1;
	D[1][0] = 0;
	
	fi >> NN >> NM >> NA >> NI;
	for (int i = 0, x, y, c; i < NM; i++)
	{
		fi >> x >> y >> c;
		M[x][y] = M[y][x] = c;
		V[x].push_back (y);
		V[y].push_back (x);
	}
	for (int i = 0, n, t, c; i < NA; i++)
	{
		fi >> n >> t >> c;
		D[n][t] = c;
	}
}

void rez ()
{
	int n, t, val, f, c, i;
	for (t = 1; t < tmax; t++)
	{
		for (n = 1; n <= NN; n++)
		{
			if (D[n][t] > 0)
				val = D[n][t];
			else
				val = 0;
			D[n][t] = -1;
			
			for (i = 0; i < V[n].size(); i++)
			{
				f = V[n][i];
				c = M[n][f];
				if (t - c < 0 || D[f][t-c] == -1) continue;
				D[n][t] = max (D[n][t], D[f][t-c]);
			}
			
			if (D[n][t-1] != -1)
				D[n][t] = max (D[n][t], D[n][t-1]);
			if (D[n][t] != -1)
				D[n][t] += val;
		}
	}
}

void afi ()
{
	for (int i = 1, n, t; i <= NI; i++)
	{
		fi >> n >> t;
		fo << D[n][t] << '\n';
	}
}

int main ()
{	
	cit ();
	rez ();
	afi ();
	return 0;
}