背包问题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 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博客_背包九讲

 

 

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值