背包问题大背景:

存在一批物品,属性有价值(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 Description
Many years ago , in Teddy’s hometown there was a man who was called “Bone Collector”. This man like to collect varies of 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 calculate out the maximum of the total value the bone collector can get ?

Input
The first line contain a integer T , the number of cases.
Followed by T cases , each case three lines , the first line contain two integer N , V, (N <= 1000 , V <= 1000 )representing the number of bones and the volume of his bag. And the second line contain N integers representing the value of each bone. The third line contain N integers representing the volume of each bone.

Output
One integer per line representing the maximum of the total value (this number will be less than 231).

输入样例

1
5 10
1 2 3 4 5
5 4 3 2 1

输出样例

14

题解代码:

#include<bits/stdc++.h>
using   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<<DP[m]<<endl;}
}

类型二:完全背包

每种物品有无限个可以选取,我们要做的只是把一维数组的滚动方式从

从后向前滚动(int j=m;j>=w[i].cost;j++)

改为从前向后滚动 (int    j=arr[i].c;j<=m;j++)

例题:

G. Piggy-Bank

题目描述

Problem Description
Before ACM can do anything, a budget must be prepared and the necessary financial support obtained. The main income for this action comes from Irreversibly Bound Money (IBM). The idea behind is simple. Whenever some ACM member has any small money, he takes all the coins and throws them into a piggy-bank. You know that this process is irreversible, the coins 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 paid.

But there is a big problem with piggy-banks. It is not possible to determine how much money is inside. So we might break the pig into pieces only to find out that there is not enough money. Clearly, we want to avoid this unpleasant situation. The only possibility is to weigh the piggy-bank and try to guess how many coins are inside. Assume that we are able to determine the weight of the pig exactly and that we know the weights of all coins of a given currency. Then there is some minimum amount of money in the piggy-bank that we can guarantee. Your task is to find out this worst case and determine the minimum amount of cash inside the piggy-bank. We need your help. No more prematurely broken pigs!

Input
The input consists of T test cases. The number of them (T) is given on the first line of the input file. Each test case begins with a line containing two integers E and F. They indicate the weight of an empty pig and of the pig filled with coins. Both weights are given in grams. No pig will weigh more than 10 kg, that means 1 <= E <= F <= 10000. On the second line of each test case, there is an integer number N (1 <= N <= 500) that gives the number of various coins used in the given currency. 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 grams.

Output
Print exactly one line of output for each test case. The line must contain the sentence “The minimum amount of money in the piggy-bank is X.” where X is the minimum amount of money that can be achieved using coins with the given total weight. If the weight cannot be reached exactly, print a line “This is impossible.”.

输入样例

3
10 110
2
1 1
30 50
10 110
2
1 1
50 30
1 6
2
10 3
20 4

输出样例

The minimum amount of money in the piggy-bank is 60.
The minimum amount of money in the piggy-bank is 100.
This is impossible.

题目这次改问最小值,只需要将原先判断的max改为min即可

特殊注意:此题增加设置了判断是否背包能满足条件的情况下刚好装满;解决方法为将dp数组的初始值先都设置为无限大,若无法满足条件则结果仍未无限大,判断之后输出即可。

解题代码:

#include<bits/stdc++.h>
using   namespace   std;
#define INFI    0x3f3f3f3f
#define SIZE    10005
struct  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]<INFI) printf("The minimum amount of money in the piggy-bank is %d.\n",dp[m]);else printf("This is impossible.%d\n");}
}

类型三:完全背包

每一件物品有可用的件数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
对于每组测试数据,请输出能够购买大米的最多重量,你可以假设经费买不光所有的大米,并且经费你可以不用完。每个实例的输出占一行。

输入样例

1
8 2
2 100 4
4 100 2

输出样例

400

还是在按照物品种类大循环,物品件数小循环的情况下,在状态转移时多加了一个有关每种物品件数的循环

代码如下:

#include<bits/stdc++.h>
using   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<<dp[m]<<endl;}return  0;
}

经典变形背包问题:固定钱币面额买东西问题;

如有200元,150元,350元三种面额的钱币,现求任给一数n元,怎样能花掉最多的m元,使得n-m有最小值。

如用贪心解决,当n=650时,会先用350,再用200,最终余下100;显然错误,不如一张350两张150;

转换思维可采用背包问题算法

例题如下:

I. 寒冰王座

题目描述

Problem Description
不死族的巫妖王发工资拉,死亡骑士拿到一张N元的钞票(记住,只有一张钞票),为了防止自己在战斗中频繁的死掉,他决定给自己买一些道具,于是他来到了地精商店前.

死亡骑士:”我要买道具!”

地精商人:”我们这里有三种道具,血瓶150块一个,魔法药200块一个,无敌药水350块一个.”

死亡骑士:”好的,给我一个血瓶.”

说完他掏出那张N元的大钞递给地精商人.

地精商人:”我忘了提醒你了,我们这里没有找客人钱的习惯的,多的钱我们都当小费收了的,嘿嘿.”

死亡骑士:”……”

死亡骑士想,与其把钱当小费送个他还不如自己多买一点道具,反正以后都要买的,早点买了放在家里也好,但是要尽量少让他赚小费.

现在死亡骑士希望你能帮他计算一下,最少他要给地精商人多少小费.

Input
输入数据的第一行是一个整数T(1<=T<=100),代表测试数据的数量.然后是T行测试数据,每个测试数据只包含一个正整数N(1<=N<=10000),N代表死亡骑士手中钞票的面值.

注意:地精商店只有题中描述的三种道具.

Output
对于每组测试数据,请你输出死亡骑士最少要浪费多少钱给地精商人作为小费.

输入样例

2
900
250

输出样例

0
50

解题代码:

#include<bits/stdc++.h>
using   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<<n-dp[n]<<endl;}
}

两个非常完善的背包问题讲解网址:

https://www.cnblogs.com/mfrank/p/10849505.html
 背包九讲----整理+例题_smilng的博客-CSDN博客_背包九讲

背包问题C++(三种类型初涉)相关推荐

  1. Asp.net支持三种类型的cache[转]

    from:http://www.cnblogs.com/thomasnet/archive/2006/11/26/573104.html Asp.net支持三种类型的cache 想写一个技术快速概述, ...

  2. java中三种转string的方法_java中int,char,string三种类型的相互转换

    如何将字串 String 转换成整数 int? int i = Integer.valueOf(my_str).intValue(); int i=Integer.parseInt(str); 如何将 ...

  3. html5中标签分为,HTML标签的三种类型

    HTML标签的类型分为三种:行内元素,行内块元素,块级元素 而标签的属性是可以转换的 display:inline: 转换为行内元素 display:linline-block 转换为行内块元素 di ...

  4. java中有scoreframe类型嘛_java构造函数的三种类型总结

    我们说构造函数能处理参数的问题,但其实也要分三种情况进行讨论.目前有三种类型:无参.有参和默认.根据不同的参数情况,需要我们分别进行构造函数的讨论.这里重点是无参构造函数的初始化也要分两种方法进行分析 ...

  5. mysql varchar,bigint,char三种类型性能的比较

    mysql varchar,bigint,char三种类型性能的比较 比较数据类型的性能好坏,数据表必须有足够的数据,我用25万条数据做测试 字段是手机号,用这三个类型哪个类型好呢.首先分析手机号有1 ...

  6. mysql double 转 字符串_没想到!在MySQL数据库中的数据有这三种类型!

    MySQL数据库是一个或多个数据列构成二维表,它的每一种数据列都有特定类型,而类型决定MySQL是怎么看待该列数据,如果把整型数值存放到字符类型的列中,MySQL则会把它当成字符串来处理. MySQL ...

  7. 辨别DVI接口连接线三种类型五种规格

    DVI(Digital Visual Interface),即数字视频接口.它是1999年由Silicon Image.Intel(英特尔).Compaq(康柏).IBM.HP(惠普).NEC.Fuj ...

  8. mysql dbms是什么_DBMS体系结构的三种类型分别是什么

    DBMS体系结构的三种类型分别是什么 发布时间:2020-12-05 13:27:28 来源:亿速云 阅读:129 作者:小新 这篇文章主要介绍了 DBMS体系结构的三种类型分别是什么,具有一定借鉴价 ...

  9. ios 开发者证书付费三种类型区别

    ios 开发者证书付费三种类型区别 苹果开发者账号分为 个人(individual),公司(company),企业(enterprise)三种类型. 1.个人开发者账号: (1)费用:99美元每年. ...

最新文章

  1. php中this,self,parent三个关键字
  2. Pytorch常见的坑汇总
  3. Google联合OpenAI揭秘神经网络黑箱:AI的智慧,都藏在激活地图里
  4. python pycocotools安装
  5. 代码片段--批量生产库以及可执行文件的依赖关系
  6. 如何用指针访问opencv cv::Mat数据?ptr<uchar>()
  7. 使用 BOOST.ASSERT 机制替换库断言
  8. C#json数据的序列化和反序列化(将数据转换为对象或对象集合)
  9. camera中文版软件 ip_ip camera网络摄像机
  10. 不重启docker容器修改 容器中的时区
  11. 使用mybatis-spring-boot-starter如何打印sql语句
  12. 小米推新,黄章怒骂!留给魅族们的时间不多了 | 畅言
  13. 初学Jmeter的摘抄学习总结----------基础知识篇
  14. cat3 utp是不是网线_网线UTP-CAT5、UTP-CAT5e、UTP-cat6产品简介讲解
  15. 在做开关电路时,三极管限流电阻该如何选择?
  16. LSI阵列卡的使用教程
  17. 【食品加工技术】第二章 果蔬加工技术 笔记
  18. 网络安全之密码安全基础
  19. 【网安入门】学习笔记(一)
  20. 黄仁勋没有回应,英伟达没有新品

热门文章

  1. 结构体内存对齐,默认对齐数,结构体传参
  2. Vivado® ML 版,让设计更智能化
  3. 相关方管理---章节练习
  4. 第9周测验-鸣人和佐助
  5. 动态白盒测试——逻辑覆盖测试法
  6. Q.me推出! Amortech展示了它的卡尔加里软件开发技能
  7. 笔记本电脑的计算机配置在哪里可以找到,如何查看笔记本电脑的配置?笔记本配置查看方法...
  8. 中芯国际二零一八年第二季度业绩公布
  9. hid read c Linux,linux/windows hid
  10. Could not GET 'http://jcenter.bintray.com/com/github/dcendents/android-maven-gradle-plugin/2.1/andro