Cod sursa(job #9802)

Utilizator gcosminGheorghe Cosmin gcosmin Data 27 ianuarie 2007 16:58:42
Problema Amenzi Scor 80
Compilator cpp Status done
Runda Unirea 2007, clasele 11-12 Marime 1.39 kb
#include <stdio.h>
#include <algorithm>
using namespace std;

#define NMAX 160
#define INF 1000000000

typedef struct jeg {
	int x, t, c;
};

int cmp(jeg a, jeg b)
{
	return (a.t < b.t);
}

int N, M, K, P;

int dist[NMAX][NMAX];

jeg a[12010];
int cast[12010];

inline int MAX(int a, int b) { return (a > b) ? a : b; }

int main()
{
	int i, j, k, x, y, c;
	
	freopen("amenzi.in", "r", stdin);
	freopen("amenzi.out", "w", stdout);

	scanf("%d %d %d %d", &N, &M, &K, &P);

	for (i = 1; i <= N; i++)
		for (j = 1; j <= N; j++) 
			if (i != j) dist[i][j] = INF;

	for (i = 1; i <= M; i++) {
		scanf("%d %d %d", &x, &y, &c);

		dist[x][y] = dist[y][x] = c;
	}

	for (k = 1; k <= N; k++)
		for (i = 1; i <= N; i++)
			for (j = 1; j <= N; j++) 
				if (dist[i][k] + dist[k][j] < dist[i][j])
					dist[i][j] = dist[i][k] + dist[k][j];

	for (i = 1; i <= K; i++) scanf("%d %d %d", &a[i].x, &a[i].t, &a[i].c);

	sort(a + 1, a + K + 1, cmp);
	a[0].x = 1;
	a[0].t = 0;
	a[0].c = 0;

	int tc, cc, xx;
	for (i = 1; i <= K; i++) {
		cast[i] = -INF;
		tc = a[i].t;
		cc = a[i].c;
		xx = a[i].x;
		for (j = i - 1; j >= 0; j--)
			if (a[j].t + dist[a[j].x][xx] <= tc)
				cast[i] = MAX(cast[i], cast[j] + cc);
	}

	int T, max;
	for (i = 1; i <= P; i++) {
		scanf("%d %d", &x, &T);
		max = 0;

		for (j = 0; j <= K; j++) 
			if (a[j].t + dist[a[j].x][x] <= T)
				if (cast[j] > max) max = cast[j];

		printf("%d\n", max);
	}

return 0;
}