Cod sursa(job #52033)

Utilizator MARCELMIHALCEA MARICEL MARCEL Data 17 aprilie 2007 17:29:19
Problema Amenzi Scor 30
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.93 kb


#include<stdio.h>
#include<stdlib.h>

#define max( x, y ) ( x < y ? y : x )

#define Mmax 1501
#define Kmax 12000
#define Nmax 151
#define Tmax 3501
#define Pmax 8001

struct sotie {
 int nod, t ;} s[Pmax];

struct amenzi {
 int nod, t, c; } am[Kmax], *G[Nmax];


int a[Nmax][Nmax],deg[Nmax],n,m,k,p;
long c[Nmax][Tmax];


int sort_function( const void *a, const void *b)
{

amenzi p,q;
p=*( amenzi * ) a;
q=*( amenzi * ) b;

return p.t-q.t;
}

long infractiune( amenzi * a, int n, int timp )
 {
  int step,poz;

  for(step=1; step <= n ;step <<= 1) ;

  for( poz=1 ; step ; step >>= 1 )
   if( poz + step <= n )
	if(  a[ poz+step ].t <= timp )
	   poz += step;

  if( a[poz].t == timp ) return a[poz].c;

  return 0;
 }


int main()
{

int i,j,x,y,cost,vf,xf,tf,tmax=0,t;
long amenda;

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

scanf("%d%d%d%d",&n,&m,&k,&p);

for(i=1;i<=m;i++)
 {
 scanf("%d%d%d",&x,&y,&cost);
 a[x][y]=a[y][x]=cost;
 }

for(i=1;i<=k;i++)
 {
 scanf("%d%d%d",&am[i].nod,&am[i].t,&am[i].c);
 deg[ am[i].nod ] ++;
 }


 for(i=1;i<=n;i++)
  G[i] = ( amenzi * ) calloc ( deg[i]+1 , sizeof(amenzi) );


	qsort((void *)(am+1), k, sizeof(am[0]), sort_function);



  for(i=1;i<=k;i++)
   {
   vf = am[i].nod;
   G[ vf ][ ++ G[vf][0].nod ] = am[i];
   }


   for( i = 1 ; i <= p ; i ++ )
	{
	scanf("%d%d",&s[i].nod,&s[i].t);
	tmax=max(tmax,s[i].t);
	}


	for(i=1;i<=n;i++)
	 for(t=0;t<=tmax;t++)
	  c[i][t]=-1;

	  c[1][0]=0;

   for( t = 1 ; t <= tmax ; t ++ )
	for( i = 1 ; i <= n ; i ++ )
   {
	   c[i][t]=c[i][t-1];

	 for(j=1;j<=n;j++)
	  if( a[i][j] )
	   if( t-a[i][j] >= 0 )
		c[i][t] = max( c[i][t] , c[j][t-a[i][j]] );

	if( c[i][t] != -1 && (amenda = infractiune(G[i],G[i][0].nod,t))  )
	 c[i][t] += amenda ;

   }


	for( i = 1 ; i <= p ; i ++ )
	  printf("%ld\n",c[s[i].nod][s[i].t]);

 return 0;
 }