Cod sursa(job #22168)

Utilizator webspiderDumitru Bogdan webspider Data 25 februarie 2007 21:36:23
Problema Amenzi Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.38 kb
#include <iostream>
#include <vector>
#include <stdio.h>

using namespace std;

int n, m, k, p;

vector<int> fii[151];
vector<int> tmp[151];
vector<int> ame[151][2];
vector<int> intl[2];

int pos[151][4501];
int amm[151][4501];

int main()
{
	int a,b,c;
	int i;
	int nodc,tmpc,j;
	int maxc;

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

	scanf("%d %d %d %d\n", &n, &m, &k, &p);

	for ( i = 1; i <= m; i++ )
	{
		scanf("%d %d %d\n", &a, &b, &c);
		fii[a].push_back(b);
		fii[b].push_back(a);

		tmp[a].push_back(c);
		tmp[b].push_back(c);
	}

	for ( i = 1; i <= k; i++ )
	{
		scanf("%d %d %d\n", &a, &b, &c);
		ame[a][0].push_back(b);
		ame[a][1].push_back(c);
	}

	pos[1][0] = 1;

	for ( tmpc = 0; tmpc <= 3500; tmpc++ )
		for ( nodc = 1; nodc <= n; nodc ++ )
	{
		if ( pos[ nodc ][ tmpc ] )
		{
			maxc = 0;
			for ( j = 0; j < ame[ nodc ][0].size(); j++ )
			{
				if ( ame[nodc][0][j] == tmpc )
					if ( maxc < ame[nodc][1][j] )
						maxc = ame[nodc][1][j];
				amm[ nodc ][ tmpc ] += maxc;
			}
			for ( j = 0; j < fii[ nodc ].size(); j++ )
			{
				pos[ fii[nodc][j] ][ tmpc + tmp[nodc][j] ] = 1;
				amm[ fii[nodc][j] ][ tmpc + tmp[nodc][j] ] = max( amm[ fii[nodc][j] ][ tmpc + tmp[nodc][j] ], amm[ nodc ][ tmpc ] );
			}
		}
	}

	for ( i = 1; i <= p; i++ )
	{
		scanf("%d %d\n", &a, &b);
		printf("%d\n", amm[ a ][ b ] );
	}

	fclose(stdin);
	fclose(stdout);

	return 0;
	
}