Cod sursa(job #17503)

Utilizator diaDiana Adrisor dia Data 16 februarie 2007 00:14:00
Problema Amenzi Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.68 kb
#include <stdio.h>
#include <algorithm>
#define TMAX 3555
#define NMAX 155
#define MMAX 1555
#define INF 666000666

using namespace std;

struct panamea
{
        int a, b, c;
}
        Muc[MMAX];

int DP[TMAX][NMAX], N, M, K, P, Amenzi[TMAX][NMAX];

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

bool operator<(const struct panamea &x, const struct panamea &y)
{
        if (x.a < y.a || (x.a == y.a && x.b < y.b)) return 1;
        return 0;
}

int main()
{
        int t, i, j, k, c;

        freopen("amenzi.in", "r", stdin);
        scanf("%d %d %d %d", &N, &M, &K, &P);

        for (t = 0; t < M; t++)
                scanf("%d %d %d", &Muc[t].a, &Muc[t].b, &Muc[t].c);

        for (t = 0; t < K; t++)
        {
                scanf("%d %d %d", &i, &j, &k);
                Amenzi[j][i] += k;
        }

        sort(&Muc[0], &Muc[M]);

        for (t = 0; t < TMAX; t++)
                for (j = 0; j < NMAX; j++) DP[t][j] = -INF;
        DP[0][1] = 0;
        for (t = 1; t < TMAX; t++)
        {
            for (i = 0; i < M; i++)
            {
                j = Muc[i].a; k = Muc[i].b; c = Muc[i].c;
                DP[t][j] = max(max(DP[t][j], (t>0)*DP[t-1][j]), (t>=c)*DP[t-c][k]);
                k = Muc[i].a; j = Muc[i].b; c = Muc[i].c;
                DP[t][j] = max(max(DP[t][j], (t>0)*DP[t-1][j]), (t>=c)*DP[t-c][k]);
            }
            for (j = 1; j <= N; j++)
                DP[t][j] += Amenzi[t][j];
        }

        freopen("amenzi.out", "w", stdout);
        while (P--)
        {
                scanf("%d %d", &i, &j);
                printf("%d\n", DP[j][i]);
        }

        return 0;
        
}