I really want to talk about it. Every topic has to work hard to rank data for two days. It is really hard for me to be a stupid and hard-working person, there are a lot of things that can only be understood-to cheer up-otherwise it will be despised
Backpack-generally speaking, it is the question of filling a backpack. Its requirement is generally to maximize its value.
01 backpack:
There are n items and a backpack weighing M. (Only one item for each type) The weight of the I-th item is W [I], and the value is V [I]. Solving which items are loaded into a backpack can maximize the total value.
Each object has two attributes: Value and weight, while the backpack has size. V [I, j] is used to represent the previous I item {u1 ...... the maximum value of a backpack with a size in the UI} (items in the backpack.
V [I, j] = 0 if I = 0 or J = 0;
V [I, j] = V [I-1, J] If j <UI. weight)
V [I, j] = max {v [I-1, J], V [I-1, J-UI. weight] + UI. value} If I> 0 and j> = UI. weight
The simplified template is not: F [v] = max {f [v], f [V-C] + w)
Full backpack:
Each item can be placed infinitely
Multiple knapsack problems:
Each item has a fixed limit on the number of times.
Two-dimensional knapsack problems:
The two-dimensional bag problem refers to two different expenses for each item. The two costs must be paid at the same time when this item is selected, there is a maximum value (Backpack capacity) for each price, and you can find the maximum value if you select an item. Set the two costs required for the I-th item to V [I] and U [I] respectively. The maximum two costs can be paid (two types of backpack capacity) the values are V and U respectively. The value of the item is W [I].
For example, hangdian ACM 2602.
#include<stdio.h>#include<string.h>#include<iostream>using namespace std;int max(int a,int b){ return a>b?a:b;}int main(){ int t,n,m,dp[10001],i,j; int a[10000],b[10000]; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m); memset(dp,0,sizeof(dp)); for(i=1;i<=n;i++) { scanf("%d",&a[i]); } for(i=1;i<=n;i++) { scanf("%d",&b[i]); } for(i=1;i<=n;i++) { for(j=m;j>=b[i];j--) { dp[j]=max(dp[j],dp[j-b[i]]+a[i]); } } cout<<dp[m]<<endl; } return 0;}
Cutting cloth by hangdian
#include <stdio.h>#include <string.h>#include <algorithm>using namespace std;struct node{ int val,wei;} a[155];int dp[155][155];int main(){ int n,m,k,s,x,y,z,i; while(~scanf("%d%d%d%d",&n,&m,&k,&s)) { for(i = 1; i<=k; i++) scanf("%d%d",&a[i].val,&a[i].wei); memset(dp,0,sizeof(dp)); for(x = 1; x<=m; x++) { for(y = 1; y<=k; y++) { for(z = 1; z<=s; z++) { int cnt = 1; while(cnt*a[y].wei<=x && cnt<=z) { dp[x][z] = max(dp[x][z],dp[x-cnt*a[y].wei][z-cnt]+cnt*a[y].val); cnt++; } } } if(dp[x][s]>=n) break; } if(x>m) printf("-1\n"); else printf("%d\n",m-x); } return 0;}
# Include <stdio. h> # include <algorithm> # include <string. h> using namespace STD; struct node {int X, Y, V;} A [20]; int DP [1005] [1005]; int main () {int I, j, k, n, x, y, T; scanf ("% d", & T); While (t --) {scanf ("% d ", & N, & X, & Y); for (I = 0; I <n; I ++) scanf ("% d ", & A [I]. x, & A [I]. y, & A [I]. v); memset (DP, 0, sizeof (DP); for (I = 0; I <= x; I ++) // two-dimensional backpack {for (j = 0; j <= y; j ++) {for (k = 0; k <n; k ++) {if (I> = A [K]. X & J> = A [K]. y) DP [I] [J] = max (DP [I] [J], max (DP [I-A [K]. x] [J] + dp [A [K]. x] [J-A [K]. y]), (DP [I] [J-A [K]. y] + dp [I-A [K]. x] [A [K]. y]) + A [K]. v); if (I> = A [K]. Y & J> = A [K]. x) DP [I] [J] = max (DP [I] [J], max (DP [I-A [K]. y] [J] + dp [A [K]. y] [J-A [K]. x]), (DP [I] [J-A [K]. x] + dp [I-A [K]. y] [A [K]. x]) + A [K]. v) ;}} printf ("% d \ n", DP [x] [Y]);} return 0 ;}
// Minimum value
# Include <stdio. h> # include <string. h> # include "cstdio" # include <iostream> using namespace STD; # define Max 10000000; struct node {int value; int weight;} A [10005]; int DP [10000]; int main () {int t, n, m, M1, M2, I, j; scanf ("% d", & T ); while (t --) {scanf ("% d", & M1, & m2); scanf ("% d", & N); M = m2-m1; for (I = 0; I <= m; I ++) DP [I] = 10000000; DP [0] = 0; for (I = 0; I <N; I ++) {scanf ("% d", & A [I]. value, & A [I]. weight) ;}for (I = 0; I <n; I ++) {for (j = A [I]. weight; j <= m; j ++) {If (DP [J]> DP [J-A [I]. weight] + A [I]. value) DP [J] = DP [J-A [I]. weight] + A [I]. value ;}} if (DP [m] = 10000000) printf ("This is impossible. \ n "); else printf (" the minimum amount of money in the piggy-bank is % d. \ n ", DP [m]);} return 0 ;}