0-1 knapsack problem time limit: 1000ms memory limit: 10000K Total time limit: 3000ms description A backpack with a capacity of C should be loaded. Select items loaded into the backpack from n items, the weight of each item I is WI, the value is pi. For a viable backpack load, the total weight of the item in the backpack cannot exceed the capacity of the backpack, and the best load is the highest value of the item loaded. Enter more than one sample, each of the inputs accounted for three lines. The first line is two integers: N (n<=10) and C, the second row n integers are w1 to WN, and the third row n integers are p1 to pn respectively.
Both N and c are equal to the zero flag input end. Outputs the output of each sample as a row, outputting an integer that is the total value of the best load. Input Example 1 2
1
1
2 3
2 2
3 4
0 0 Output Example 1
4 for the 0-1 knapsack problem, the greedy strategy has four kinds commonly used. Each greedy strategy uses a multi-step process to complete the backpack loading. Use the greedy criteria in each step to select an item to pack in the backpack. A greedy rule: from the rest of the items, select the most valuable items that can be loaded into the backpack, using the rules, the most valuable items are first loaded (assuming there is sufficient capacity), and then the next most valuable item, so continue. This strategy is not guaranteed to get the optimal solution. The second guideline selects from the rest of the items the smallest item that can be loaded into the backpack so that it continues until the condition is not met. The optimal solution may not be obtained under normal circumstances. The third is the value density (value-to-weight ratio pi/wi) greedy algorithm, the selection criterion is: From the remainder of the items can be loaded into the package Pi/wi value of the largest items. This strategy is not guaranteed to get the optimal solution. The fourth type is heuristic greedy algorithm. The following code uses greedy strategy one:
#include <iostream>using namespace std; typedef struct mine{int weight; The weight of the item int price; The value of the item} mine; int main () {int num,container; Mine data[25]; cin>>num>>container; while (true) {if (num==0&&container==0) break; for (int i = 0;i < num; i++) cin>>data[i].weight; for (i = 0;i < num; i++) cin>>data[i].price; for (int m = 0;m < num-1; m++)//Sort items by value for (int n = 0;n < num-m-1; n++) if (data[n].price<data[n+1].price) {mine temp; Temp.price = Data[n].price; Temp.weight = Data[n].weight; Data[n].price = Data[n+1].price; Data[n].weight = Data[n+1].weight; Data[n+1].price = Temp.price;Data[n+1].weight = Temp.weight; } int sumweight = 0; int sumprice = 0; for (int k = 0;k < num; k++) if (data[k].weight<= (container-sumweight)) {SUMPR Ice + = Data[k].price; Sumweight + = Data[k].weight; } cout<<sumprice<<endl; cin>>num>>container; } return 0;}
It is not guaranteed that the optimal solution is obtained, for example:
Input:
10 100
4 5 6 6 7 7 8 9 11 45
7 9 11 12 13 14 15 17 21 89
Output is 192
Using dynamic programming Algorithms:
#include <iostream>using namespace Std;int main () { int n,c,i,j; cin>>n>>c; while (n!=0| | c!=0) { int *w =new int[n]; int *p =new int[n]; int **a = new Int*[n+1]; for (i=0;i<=n;i++) a[i]= new int[c+1]; for (i=0;i<n;i++) cin>>w[i]; for (j=0;j<n;j++) cin>>p[j]; for (int k=0;k<c+1;k++) a[0][k] = 0; for (i=0;i<n;i++) for (j=0;j<=c;j++) { if (w[i]>j) a[i+1][j] = a[i][j]; else if (A[i][j]<a[i][j-w[i]]+p[i]) a[i+1][j] = A[i][j-w[i]]+p[i]; else a[i+1][j] = A[i][j]; } cout<<a[n][c]<<endl; cin>>n>>c; } return 0;}
For the same input can be obtained 193, because the weight of 4 and 5 of items can be replaced by a weight of 11 items, so as to achieve maximum value.
0-1 knapsack problem