Cod sursa(job #689523)

Utilizator GrimpowRadu Andrei Grimpow Data 24 februarie 2012 16:59:31
Problema Amenzi Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.77 kb
#include<cstdio>

#define maxtime 3501
#define maxN 151
#define maxM 1501
#define maxP 8001

long N, M, K, P, Tmax, A[maxtime][maxN], nd[maxP], time[maxP];
long B[maxtime][maxN];

struct nod
{
       long nd;
       long c;
       nod *next;
} *L[maxN], *v;

void add(nod *&L, long x, long c)
{
     nod *camp = new nod;
     camp -> nd = x;
     camp -> c = c;
     camp -> next = L;
     L = camp;
}

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

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

    long i, x, y, c, t;

    for(i=1; i<=M; ++i)
    {
             scanf("%ld %ld %ld", &x, &y, &c);
             add(L[x], y, c);
             add(L[y], x, c);
    }

    for(i=1; i<=K; ++i)
    {
             scanf("%ld %ld %ld", &x, &t, &c);
             A[t][x] += c;
    }

    for(i=1; i<=P; ++i)
    {
             scanf("%ld %ld", nd+i, time+i);
             Tmax = time[i] > Tmax ? time[i] : Tmax;
    }

    for(x=2; x<=N; ++x)
             B[0][x] = -1;

    for(t=1; t<=Tmax; ++t)
    {
             for(x=1; x<=N; ++x)
             {
                      v = L[x];
					  B[t][x] = B[t-1][x];

                      while(v)
                      {
                              y = v -> nd;
                              c = v -> c;

                              if(c <= t && B[t-c][y] > B[t][x])
                                   B[t][x] = B[t-c][y];

                              v = v -> next;
                      }

                      if(B[t][x] != -1)
                                 B[t][x] += A[t][x];
             }
    }

    for(i=1; i<=P; ++i)
             printf("%ld\n", B[time[i]][nd[i]]);

    fclose(stdin); fclose(stdout);
    return 0;
}