Cod sursa(job #494939)

Utilizator xtremespeedzeal xtreme Data 23 octombrie 2010 13:26:58
Problema Energii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1 kb
#include <stdio.h>
#include <assert.h>
#define Wmax 5000
#define INF 2000000000
#define Gmax 1000

int G, W, cost[Wmax+1], req[Gmax+1], gain[Gmax+1];

void read()
      {
	   int i;		
	   
	   freopen("energii.in","r",stdin);
	   
	   scanf("%d%d",&G,&W);
	   for(i=1;i<=G;i++)
		        scanf("%d %d",&gain[i],&req[i]);
	   
	   fclose(stdin);
	   
	   for(i=1;i<=W;i++)
		      cost[i]=INF;
	   
	  }
void solve()
	{
	 int i, j;	 
		
	 for(i=1;i<=G;i++)
		 for(j=W-1;j>=0;j--)
			 if(cost[j]!=INF)
				 {
			      if(j+gain[i]>=W)
					     {
					      if(cost[W]>cost[j]+req[i])
						        cost[W]=cost[j]+req[i];
						 }
				  else if(cost[j+gain[i]]>cost[j]+req[i])
				                cost[j+gain[i]]=cost[j]+req[i];
				 }
	}
void write()
	{
		
	 freopen("energii.out","w",stdout);
	 
	 if(cost[W]==INF) 
		     printf("-1\n");
	 else 
		     printf("%d\n",cost[W]);
	 
	 fclose(stdout);
	}
int main()
	{
	 read();
	 solve();
	 write();
	 return 0;
	}