Cod sursa(job #491443)

Utilizator xtremespeedzeal xtreme Data 11 octombrie 2010 12:42:24
Problema Energii Scor 95
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.13 kb
#include <stdio.h>
#include <assert.h>
#define Wmax 5000
#define INF 2000000000
#define Gmax 1000

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

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