Cod sursa(job #28882)

Utilizator ionescu_bogdanIonescu Bogdan-Gabriel ionescu_bogdan Data 8 martie 2007 13:49:04
Problema Amenzi Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.19 kb
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define tmax 3510
#define nmax 155
#define oo 1000000000
#define max(a,b) (a>b?a:b)

struct nod
{
    int x,c;
    nod *n;
};

int n,m,k,p,am[tmax][nmax],bst[tmax][nmax],i,j,a,b,c;
nod *adj[nmax],*aux;

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

    scanf("%d%d%d%d",&n,&m,&k,&p);
    for (i=0;i<m;i++)
    {
        scanf("%d%d%d",&a,&b,&c);
        aux=new nod;
        aux->x=b,aux->c=c,aux->n=adj[a],adj[a]=aux;
        aux=new nod;
        aux->x=a,aux->c=c,aux->n=adj[b],adj[b]=aux;
    }
    for (i=0;i<k;i++)
    {
        scanf("%d%d%d",&a,&b,&c);
        am[b][a]+=c;
    }
    bst[0][1]=0;
    for (i=2;i<=n;i++)
        bst[0][i]=-oo;
    for (i=1;i<=3500;i++)
        for (j=1;j<=n;j++)
        {
            bst[i][j]=bst[i-1][j];
            for (aux=adj[j];aux!=NULL;aux=aux->n)
                if (i>=aux->c)
                    bst[i][j]=max(bst[i][j],bst[i-aux->c][aux->x]);
            bst[i][j]+=am[i][j];
        }
    for (i=0;i<p;i++)
    {
        scanf("%d%d",&a,&b);
        printf("%d\n",bst[b][a]);
    }
        
    return 0;
}