Cod sursa(job #9895)

Utilizator gcosminGheorghe Cosmin gcosmin Data 27 ianuarie 2007 18:54:09
Problema Amenzi Scor 70
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.54 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];

int nod[12010];
int timp[12010];
int cost[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);

	for (i = 1; i <= K; i++) 
		nod[i] = a[i].x, timp[i] = a[i].t, cost[i] = a[i].c;
	
	nod[0] = 1;
	timp[0] = 0;
	cost[0] = 0;

	int tc, cc, xx;
	for (i = 1; i <= K; i++) {
		cast[i] = -INF;
		tc = timp[i];
		cc = cost[i];
		xx = nod[i];

		for (j = 0; j < i; j++)
			if (timp[j] + dist[nod[j]][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 && timp[j] <= T; j++) 
			if (timp[j] + dist[nod[j]][x] <= T)
				if (cast[j] > max) max = cast[j];

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

return 0;
}