Http://www.cnblogs.com/qinyg/archive/2012/04/26/2471829.html
Problem Description:
Given n items and a backpack. The weight of item I is WI, its value bit VI, the capacity of the backpack is C. Ask how to choose the items loaded into the backpack, so that the total value of the items transferred to the backpack is the largest??
When selecting an item, there are only two options for each item I put in the backpack or not in the backpack. Can not say goods I loaded several times, nor can only be loaded into the part of the item. Therefore, the problem is called the 0-1 knapsack problem.
Problem Analysis: V (i,j) indicates that the maximum value of an item in a backpack with a capacity of J (1<=j<=c) can be loaded in the first I (1<=I<=N) item, and the following dynamic planning functions are available:
(1) V (i,0) =v (0,j) =0
(2) V (i,j) =v (i-1,j) J<wi
V (i,j) =max{v (i-1,j), V (I-1,J-WI) +vi)} J>wi
(1) type indicates: If the weight of the item I is greater than the capacity of the backpack, then the maximum value that can be obtained by the front I item is the same as the maximum price to be loaded before the i-1 item, i.e. the item I cannot be loaded into the backpack; the first (2) formula indicates that if the weight of item I is less than the capacity of the backpack, there are (a) If the item I is loaded into a backpack, the value of the backpack item is equal to the value of the J-WI in the backpack of the i-1 item, plus the value of the article I, VI; (b) If item I is not loaded into a backpack, the value of the item in the backpack is equal to the value obtained in carrying the first i-1 item into a backpack of capacity J. Obviously, taking the most value of the two is the best solution for loading the first I item into a backpack with a capacity of J.
#include <stdio.h>
int v[200][200];//The maximum value obtained in a backpack with a capacity of J before I load
int max (int a,int b)
{
if (a>=b)
return A;
else return B;
}
int knapsack (int n,int w[],int v[],int x[],int C)
{
int i,j;
for (i=0;i<=n;i++)
v[i][0]=0;
for (j=0;j<=c;j++)
v[0][j]=0;
for (i=0;i<=n-1;i++)
for (j=0;j<=c;j++)
if (J<w[i])
V[I][J]=V[I-1][J];
Else
V[i][j]=max (V[i-1][j],v[i-1][j-w[i]]+v[i]);
J=c;
for (i=n-1;i>=0;i--)
{
if (V[i][j]>v[i-1][j])
{
X[i]=1;
J=j-w[i];
}
Else
x[i]=0;
}
printf ("The selected item is: \ n");
for (i=0;i<n;i++)
printf ("%d", x[i]);
printf ("\ n");
return V[N-1][C];
}
int main ()
{
The maximum value obtained by int s;//
int w[15];//The weight of the item
Value of int v[15];//items
int x[15];//The selected state of the item
int n,i;
int c;//Backpack Max capacity
n=5;
printf ("Please enter the maximum capacity of the backpack: \ n");
scanf ("%d", &c);
printf ("Number of items entered: \ n");
scanf ("%d", &n);
printf ("Please enter the weight of the item separately: \ n");
for (i=0;i<n;i++)
scanf ("%d", &w[i]);
printf ("Please enter the value of the item separately: \ n");
for (i=0;i<n;i++)
scanf ("%d", &v[i]);
S=knapsack (N,W,V,X,C);
printf ("Maximum item value: \ n");
printf ("%d\n", s);
return 0;
}
01 knapsack problem