背包问题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,求解放⼊哪些物品可以使得背包中总价值最⼤。
include和contain多重背包:
有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 g
uess 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;
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论