Exemplo n.º 1
0
int Knapsack(int n,int w)
{
      int x,y;
      if(n<0 || !w) return 0;
      if(dp[n][w]!=-1) return dp[n][w];
      x=Knapsack(n-1,w);
      y=((w>=d[n]) ? (p[n]+Knapsack(n-1,w-d[n])):0);
      used[n][w]=(y>x);
      return dp[n][w]=((x>y)?x:y);
}
Exemplo n.º 2
0
void Knap<Tw, Tp>::Knapsack(int i)
{// Search from level i node.
   if (i > n) {// at a leaf
      bestp = cp;
      return;}
   // check subtrees
   if (cw + w[i] <= c) {// try x[i] = 1
      cw += w[i];
      cp += p[i];
      Knapsack(i+1);
      cw -= w[i];
      cp -= p[i];}
   if (Bound(i+1) > bestp) // try x[i] = 0
      Knapsack(i+1);
}
void main()
{
	int m,n,i,j,index;
	float r,s,temp;
	printf("\nEnter the limit of the sack:");
	scanf("%f",&u);
	printf("\nEnter the no. of objects:");
	scanf("%d",&n);
	printf("\nEnter the weight and the profit of the objects one by one:");
	for(i=0;i<n;i++)
	{
		printf("\n\nEnter the weight:");
		scanf("%f",&wt[i]);
		printf("\nEnter the profit:");
		scanf("%f",&pr[i]);
		rat[i]=pr[i]/wt[i];
		srat[i]=pr[i]/wt[i];
	}
	
	for(i=0;i<n-1;i++)
	{
		index=i;
		for(j=i+1;j<n;j++)
		{
			if(srat[index]<srat[j])
			{
				temp=srat[index];
				srat[index]=srat[j];
				srat[j]=temp;
		    }
	    }
	}
	
	Knapsack(n,u,wt,pr,rat,srat);
}
Exemplo n.º 4
0
int main()
{
	int i,j,k,l,m,n,t,x,y,w,blank=0,test=0,mark[SZ];
      while(scanf("%d %d",&t,&w)!=EOF)
      {
            if(blank) printf("\n");
            blank=1;
            scanf("%d",&n);
            clear(dp,-1);
            clear(used,0);
            clear(mark,0);
      	for(i=0;i<n;i++)
      	{
      	      scanf("%d %d",&d[i],&p[i]);
      	      d[i]=3*w*d[i];
      	}
      	x=Knapsack(n-1,t);
      	printf("%d\n",x);
      	x=0,i=n-1,j=t;
      	while(i>=0)
      	{
      		if(used[i][j])
      		{
      		      x++;
      		      mark[i]=1;
      		      j-=d[i];
      		}
      		i--;
      	}
      	printf("%d\n",x);
      	for(i=0;i<n;i++) if(mark[i]) printf("%d %d\n",d[i]/3/w,p[i]);
      }
	return 0;
}
Exemplo n.º 5
0
int main(){

float m=20;
Knapsack(m);


return 0;
}
Exemplo n.º 6
0
void main(void)
{
   int p[6] = {0, 6, 3, 5, 4, 6};
   int w[6] = {0, 2, 2, 6, 5, 4};
   int n = 5;
   int c = 10;
   cout << "Optimal value is" << endl;
   cout << Knapsack(p,w,c,n) << endl;
}
Exemplo n.º 7
0
void main(void)
{
   int p[6] = {0, 6, 3, 5, 4, 6};
   int w[6] = {0, 2, 2, 6, 5, 4};
   int bestx[6];
   int n = 5;
   int c = 10;
   cout << "Optimal profit is " << Knapsack(p,w,c,n,bestx) << endl;
   cout << "Packing vector is" << endl;
   for (int i=1; i <= n; i++)
      cout << bestx[i] << ' ';
   cout << endl;
}
Exemplo n.º 8
0
int main()
{
    int i, j;
    Knapsack();
    for(i = 0; i <= N; ++i)
    {
        for(j = 0; j <= C; ++j)
        {
            printf("%3d", V[i][j]);
        }
        printf("\n");
    }
    return 0;
}
int main()
{
    int items;
    scanf("%d",&items);
    int weight[items+1],value[items+1];
    int iter;
    for(iter=1; iter<=items; iter++)
    {
        scanf("%d%d",&weight[iter],&value[iter]);
    }
    int maxWeight;
    scanf("%d",&maxWeight);
    printf("Max value attained can be %d\n",Knapsack(items,weight,value,maxWeight));
}
Exemplo n.º 10
0
int main()
{

    int w[5]={0,2,1,3,2};
    int v[5]={0,12,10,20,15};
    int capacity =5;

    printf("Knapsack 0-1\n");
    Knapsack(w,v,5,5);

    printf("\nKnapsack Duplicate Item alloed\n");

    KnapsackMulti(w,v,5,5);


    int coins[4]={0,1,4,6};

    printf("\nMaking Change using DP\n");

    MakingChange(coins,5,10);
    return 0;
}
Exemplo n.º 11
0
void main(void)
{
   int p[6] = {0, 6, 3, 5, 4, 6};
   int w[6] = {0, 2, 2, 6, 5, 4};
   int x[6];
   int **f;
   int n = 5;
   int c = 10;
   Make2DArray(f, n+1, c+1);
   Knapsack(p, w, c, n, f);
   cout << "Optimal value is ";
   cout << f[1][c] << endl;
   cout << "Rest of table is" << endl;
   for (int i = 2; i <= n; i++) {
      for (int j = 0; j <= c; j++)
         cout << f[i][j] << ' ';
      cout << endl;}
   Traceback(f,w,c,n,x);
   for (int i = 1; i <= n; i++)
      cout << x[i] << ' ';
   cout << endl;
}