2023年8月1日发(作者:)

背包问题C++(三种类型初涉)背包问题⼤背景:存在⼀批物品,属性有价值(value)和内存(cost)背包有总内存由此,我们可以列出装不同东西的递推表格(DP问题特性)如下,有物品价值5,4,3,2,1 分别对应内存 1,2,3,4,5背包总内存为10。在每⼀件物品只能取⼀次的情况下可列出下表接下来讨论的不同类型背包问题其实就是不同情况列表⽅式的不同。三种背包类型:01背包:有N件物品和⼀个容量为V的背包,第i件物品消耗的容量为Ci,价值为Wi,求解放⼊哪些物品可以使得背包中总价值最⼤。完全背包:有N种物品和⼀个容量为V的背包,每种物品都有⽆限件可⽤,第i件物品消耗的容量为Ci,价值为Wi,求解放⼊哪些物品可以使得背包中总价值最⼤。多重背包:有N种物品和⼀个容量为V的背包,第i种物品最多有Mi件可⽤,每件物品消耗的容量为Ci,价值为Wi,求解⼊哪些物品可以使得背包中总价值最⼤。类型⼀:01背包此类问题物体的选取为 取 或 不取。每件物品只有1件,取了变为0,不取就还为1。状态转移每次只与上⼀层有关,所以⽤⼀个⼀维数组就可以转移⽅程:dp[i]=max(dp[i],dp[i-v[i]]+w[i])

例题:F. Bone Collector题⽬描述Problem DescriptionMany years ago , in Teddy’s hometown there was a man who was called “Bone Collector”. This man like to collect variesof bones , such as dog’s , cow’s , also he went to the grave …The bone collector had a big bag with a volume of V ,and along his trip of collecting there are a lot of bones , obviously ,different bone has different value and different volume, now given the each bone’s value along his trip , can you calculateout the maximum of the total value the bone collector can get ?InputThe first line contain a integer T , the number of ed by T cases , each case three lines , the first line contain two integer N , V, (N <= 1000 , V <= 1000 )representingthe number of bones and the volume of his bag. And the second line contain N integers representing the value of eachbone. The third line contain N integers representing the volume of each One integer per line representing the maximum of the total value (this number will be less than 231).输⼊样例15 101 2 3 4 55 4 3 2 1输出样例14题解代码:#includeusing namespace std;struct bage{ int value;//每个⾻头对应价值

int cost; //每个⾻头对应体积

}w[1005];long long DP[1005];//当前第i个物品,背包容量为j的最优值int main(){ int N; cin>>N; while(N--) { int n,m;//⾻头数量,袋⼦体积

memset(DP,0,sizeof(DP));

cin>>n>>m; for(int i=1;i<=n;i++) cin>>w[i].value; for(int i=1;i<=n;i++) cin>>w[i].cost; for(int i=1;i<=n;i++) for(int j=m;j>=w[i].cost;j--)//从后往前滚动 DP[j]=max(DP[j],DP[j-w[i].cost]+w[i].value); cout<=w[i].cost;j++)改为从前向后滚动 (int j=arr[i].c;j<=m;j++)例题:G. Piggy-Bank题⽬描述Problem DescriptionBefore ACM can do anything, a budget must be prepared and the necessary financial support obtained. The main incomefor this action comes from Irreversibly Bound Money (IBM). The idea behind is simple. Whenever some ACM member hasany small money, he takes all the coins and throws them into a piggy-bank. You know that this process is irreversible, thecoins cannot be removed without breaking the pig. After a sufficiently long time, there should be enough cash in the piggy-bank to pay everything that needs to be there is a big problem with piggy-banks. It is not possible to determine how much money is inside. So we might breakthe pig into pieces only to find out that there is not enough money. Clearly, we want to avoid this unpleasant situation. Theonly possibility is to weigh the piggy-bank and try to guess how many coins are inside. Assume that we are able todetermine the weight of the pig exactly and that we know the weights of all coins of a given currency. Then there is someminimum amount of money in the piggy-bank that we can guarantee. Your task is to find out this worst case and determinethe minimum amount of cash inside the piggy-bank. We need your help. No more prematurely broken pigs!InputThe input consists of T test cases. The number of them (T) is given on the first line of the input file. Each test case beginswith a line containing two integers E and F. They indicate the weight of an empty pig and of the pig filled with coins. Bothweights are given in grams. No pig will weigh more than 10 kg, that means 1 <= E <= F <= 10000. On the second line ofeach test case, there is an integer number N (1 <= N <= 500) that gives the number of various coins used in the givencurrency. Following this are exactly N lines, each specifying one coin type. These lines contain two integers each, Pand W (1<= P <= 50000, 1 <= W <=10000). P is the value of the coin in monetary units, W is it’s weight in Print exactly one line of output for each test case. The line must contain the sentence “The minimum amount of money inthe piggy-bank is X.” where X is the minimum amount of money that can be achieved using coins with the given totalweight. If the weight cannot be reached exactly, print a line “This is impossible.”.输⼊样例310 11021 130 5010 11021 150 301 6210 320 4输出样例The minimum amount of money in the piggy-bank is minimum amount of money in the piggy-bank is is impossible.题⽬这次改问最⼩值,只需要将原先判断的max改为min即可特殊注意:此题增加设置了判断是否背包能满⾜条件的情况下刚好装满;解决⽅法为将dp数组的初始值先都设置为⽆限⼤,若⽆法满⾜条件则结果仍未⽆限⼤,判断之后输出即可。

解题代码:

#includeusing namespace std;#define INFI 0x3f3f3f3f#define SIZE 10005struct coin{ int v;//价值

int c;//所耗体积

}arr[600];int mini(int a,int b){ return a<=b?a:b;}int dp[SIZE];int main(){ int N; cin>>N; while(N--) { int E,F; scanf("%d%d",&E,&F); //初始化dp数组

memset(dp,0,sizeof(dp)); int m=F-E;//所能承受体积 for(int i=1;i<=m;i++) dp[i]=INFI;

int n; cin>>n; for(int i=1;i<=n;i++) cin>>arr[i].v>>arr[i].c;

for(int i=1;i<=n;i++) for(int j=arr[i].c;j<=m;j++) dp[j]=mini(dp[j],dp[j-arr[i].c]+arr[i].v); if(dp[m]

printf("The minimum amount of money in the piggy-bank is %d.n",dp[m]); else

printf("This is impossible.%dn"); }}类型三:完全背包每⼀件物品有可⽤的件数n

例题如下:题⽬描述Problem Description急!灾区的⾷物依然短缺!为了挽救灾区同胞的⽣命,⼼系灾区同胞的你准备⾃⼰采购⼀些粮⾷⽀援灾区,现在假设你⼀共有资⾦n元,⽽市场有m种⼤⽶,每种⼤⽶都是袋装产品,其价格不等,并且只能整袋购买。请问:你⽤有限的资⾦最多能采购多少公⽄粮⾷呢?Input输⼊数据⾸先包含⼀个正整数C,表⽰有C组测试⽤例,每组测试⽤例的第⼀⾏是两个整数n和m(1<=n<=100, 1<=m<=100),分别表⽰经费的⾦额和⼤⽶的种类,然后是m⾏数据,每⾏包含3个数p,h和c(1<=p<=20,1<=h<=200,1<=c<=20),分别表⽰每袋的价格、每袋的重量以及对应种类⼤⽶的袋数。Output对于每组测试数据,请输出能够购买⼤⽶的最多重量,你可以假设经费买不光所有的⼤⽶,并且经费你可以不⽤完。每个实例的输出占⼀⾏。输⼊样例18 22 100 44 100 2

输出样例400还是在按照物品种类⼤循环,物品件数⼩循环的情况下,在状态转移时多加了⼀个有关每种物品件数的循环代码如下:#includeusing namespace std;struct rice{ int v; int c; int n;}arr[105];int dp[105];int main(){ int N; cin>>N; while(N--) { int m,n;//含有的资⾦总数,⼤⽶的种类 cin>>m>>n; memset(dp,0,sizeof(dp)); for(int i=1;i<=n;i++) cin>>arr[i].c>>arr[i].v>>arr[i].n; for(int i=1;i<=n;i++) for(int j=m;j>=arr[i].c;j--) for(int k=1;k<=arr[i].n&&j>=k*arr[i].c;k++) dp[j]=max(dp[j],dp[j-k*arr[i].c]+k*arr[i].v); cout<

输出样例050解题代码:#includeusing namespace std;int dp[100000];int main(){ int arr[4]={0,150,200,350}; int N; cin>>N; while(N--) { int n; cin>>n; memset(dp,0,sizeof(dp)); for(int i=1;i<=3;i++) for(int j=arr[i];j<=n;j++) dp[j]=max(dp[j],dp[j-arr[i]]+arr[i]); cout<

2023年8月1日发(作者:)

背包问题C++(三种类型初涉)背包问题⼤背景:存在⼀批物品,属性有价值(value)和内存(cost)背包有总内存由此,我们可以列出装不同东西的递推表格(DP问题特性)如下,有物品价值5,4,3,2,1 分别对应内存 1,2,3,4,5背包总内存为10。在每⼀件物品只能取⼀次的情况下可列出下表接下来讨论的不同类型背包问题其实就是不同情况列表⽅式的不同。三种背包类型:01背包:有N件物品和⼀个容量为V的背包,第i件物品消耗的容量为Ci,价值为Wi,求解放⼊哪些物品可以使得背包中总价值最⼤。完全背包:有N种物品和⼀个容量为V的背包,每种物品都有⽆限件可⽤,第i件物品消耗的容量为Ci,价值为Wi,求解放⼊哪些物品可以使得背包中总价值最⼤。多重背包:有N种物品和⼀个容量为V的背包,第i种物品最多有Mi件可⽤,每件物品消耗的容量为Ci,价值为Wi,求解⼊哪些物品可以使得背包中总价值最⼤。类型⼀:01背包此类问题物体的选取为 取 或 不取。每件物品只有1件,取了变为0,不取就还为1。状态转移每次只与上⼀层有关,所以⽤⼀个⼀维数组就可以转移⽅程:dp[i]=max(dp[i],dp[i-v[i]]+w[i])

例题:F. Bone Collector题⽬描述Problem DescriptionMany years ago , in Teddy’s hometown there was a man who was called “Bone Collector”. This man like to collect variesof bones , such as dog’s , cow’s , also he went to the grave …The bone collector had a big bag with a volume of V ,and along his trip of collecting there are a lot of bones , obviously ,different bone has different value and different volume, now given the each bone’s value along his trip , can you calculateout the maximum of the total value the bone collector can get ?InputThe first line contain a integer T , the number of ed by T cases , each case three lines , the first line contain two integer N , V, (N <= 1000 , V <= 1000 )representingthe number of bones and the volume of his bag. And the second line contain N integers representing the value of eachbone. The third line contain N integers representing the volume of each One integer per line representing the maximum of the total value (this number will be less than 231).输⼊样例15 101 2 3 4 55 4 3 2 1输出样例14题解代码:#includeusing namespace std;struct bage{ int value;//每个⾻头对应价值

int cost; //每个⾻头对应体积

}w[1005];long long DP[1005];//当前第i个物品,背包容量为j的最优值int main(){ int N; cin>>N; while(N--) { int n,m;//⾻头数量,袋⼦体积

memset(DP,0,sizeof(DP));

cin>>n>>m; for(int i=1;i<=n;i++) cin>>w[i].value; for(int i=1;i<=n;i++) cin>>w[i].cost; for(int i=1;i<=n;i++) for(int j=m;j>=w[i].cost;j--)//从后往前滚动 DP[j]=max(DP[j],DP[j-w[i].cost]+w[i].value); cout<=w[i].cost;j++)改为从前向后滚动 (int j=arr[i].c;j<=m;j++)例题:G. Piggy-Bank题⽬描述Problem DescriptionBefore ACM can do anything, a budget must be prepared and the necessary financial support obtained. The main incomefor this action comes from Irreversibly Bound Money (IBM). The idea behind is simple. Whenever some ACM member hasany small money, he takes all the coins and throws them into a piggy-bank. You know that this process is irreversible, thecoins cannot be removed without breaking the pig. After a sufficiently long time, there should be enough cash in the piggy-bank to pay everything that needs to be there is a big problem with piggy-banks. It is not possible to determine how much money is inside. So we might breakthe pig into pieces only to find out that there is not enough money. Clearly, we want to avoid this unpleasant situation. Theonly possibility is to weigh the piggy-bank and try to guess how many coins are inside. Assume that we are able todetermine the weight of the pig exactly and that we know the weights of all coins of a given currency. Then there is someminimum amount of money in the piggy-bank that we can guarantee. Your task is to find out this worst case and determinethe minimum amount of cash inside the piggy-bank. We need your help. No more prematurely broken pigs!InputThe input consists of T test cases. The number of them (T) is given on the first line of the input file. Each test case beginswith a line containing two integers E and F. They indicate the weight of an empty pig and of the pig filled with coins. Bothweights are given in grams. No pig will weigh more than 10 kg, that means 1 <= E <= F <= 10000. On the second line ofeach test case, there is an integer number N (1 <= N <= 500) that gives the number of various coins used in the givencurrency. Following this are exactly N lines, each specifying one coin type. These lines contain two integers each, Pand W (1<= P <= 50000, 1 <= W <=10000). P is the value of the coin in monetary units, W is it’s weight in Print exactly one line of output for each test case. The line must contain the sentence “The minimum amount of money inthe piggy-bank is X.” where X is the minimum amount of money that can be achieved using coins with the given totalweight. If the weight cannot be reached exactly, print a line “This is impossible.”.输⼊样例310 11021 130 5010 11021 150 301 6210 320 4输出样例The minimum amount of money in the piggy-bank is minimum amount of money in the piggy-bank is is impossible.题⽬这次改问最⼩值,只需要将原先判断的max改为min即可特殊注意:此题增加设置了判断是否背包能满⾜条件的情况下刚好装满;解决⽅法为将dp数组的初始值先都设置为⽆限⼤,若⽆法满⾜条件则结果仍未⽆限⼤,判断之后输出即可。

解题代码:

#includeusing namespace std;#define INFI 0x3f3f3f3f#define SIZE 10005struct coin{ int v;//价值

int c;//所耗体积

}arr[600];int mini(int a,int b){ return a<=b?a:b;}int dp[SIZE];int main(){ int N; cin>>N; while(N--) { int E,F; scanf("%d%d",&E,&F); //初始化dp数组

memset(dp,0,sizeof(dp)); int m=F-E;//所能承受体积 for(int i=1;i<=m;i++) dp[i]=INFI;

int n; cin>>n; for(int i=1;i<=n;i++) cin>>arr[i].v>>arr[i].c;

for(int i=1;i<=n;i++) for(int j=arr[i].c;j<=m;j++) dp[j]=mini(dp[j],dp[j-arr[i].c]+arr[i].v); if(dp[m]

printf("The minimum amount of money in the piggy-bank is %d.n",dp[m]); else

printf("This is impossible.%dn"); }}类型三:完全背包每⼀件物品有可⽤的件数n

例题如下:题⽬描述Problem Description急!灾区的⾷物依然短缺!为了挽救灾区同胞的⽣命,⼼系灾区同胞的你准备⾃⼰采购⼀些粮⾷⽀援灾区,现在假设你⼀共有资⾦n元,⽽市场有m种⼤⽶,每种⼤⽶都是袋装产品,其价格不等,并且只能整袋购买。请问:你⽤有限的资⾦最多能采购多少公⽄粮⾷呢?Input输⼊数据⾸先包含⼀个正整数C,表⽰有C组测试⽤例,每组测试⽤例的第⼀⾏是两个整数n和m(1<=n<=100, 1<=m<=100),分别表⽰经费的⾦额和⼤⽶的种类,然后是m⾏数据,每⾏包含3个数p,h和c(1<=p<=20,1<=h<=200,1<=c<=20),分别表⽰每袋的价格、每袋的重量以及对应种类⼤⽶的袋数。Output对于每组测试数据,请输出能够购买⼤⽶的最多重量,你可以假设经费买不光所有的⼤⽶,并且经费你可以不⽤完。每个实例的输出占⼀⾏。输⼊样例18 22 100 44 100 2

输出样例400还是在按照物品种类⼤循环,物品件数⼩循环的情况下,在状态转移时多加了⼀个有关每种物品件数的循环代码如下:#includeusing namespace std;struct rice{ int v; int c; int n;}arr[105];int dp[105];int main(){ int N; cin>>N; while(N--) { int m,n;//含有的资⾦总数,⼤⽶的种类 cin>>m>>n; memset(dp,0,sizeof(dp)); for(int i=1;i<=n;i++) cin>>arr[i].c>>arr[i].v>>arr[i].n; for(int i=1;i<=n;i++) for(int j=m;j>=arr[i].c;j--) for(int k=1;k<=arr[i].n&&j>=k*arr[i].c;k++) dp[j]=max(dp[j],dp[j-k*arr[i].c]+k*arr[i].v); cout<

输出样例050解题代码:#includeusing namespace std;int dp[100000];int main(){ int arr[4]={0,150,200,350}; int N; cin>>N; while(N--) { int n; cin>>n; memset(dp,0,sizeof(dp)); for(int i=1;i<=3;i++) for(int j=arr[i];j<=n;j++) dp[j]=max(dp[j],dp[j-arr[i]]+arr[i]); cout<