# 背包问题C++（三种类型初涉）

分别对应内存 1，2，3，4，5

01背包：

# 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;
}
}

# 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.

#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");
}
}

Problem Description

Input

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;
}

I. 寒冰王座

Problem Description

Input

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
点赞
• 3
收藏
• 打赏
• 0
评论
09-03
08-31
11-27
10-20
07-20 824
07-18 296
11-24 508
01-11 93
07-18 184
03-24 3万+
01-03 39
03-18 7183

### “相关推荐”对你有帮助么？

• 非常没帮助
• 没帮助
• 一般
• 有帮助
• 非常有帮助

©️2022 CSDN 皮肤主题：深蓝海洋 设计师：CSDN官方博客

pied_piperG

¥2 ¥4 ¥6 ¥10 ¥20

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