完全背包简介

背包问题

背包问题是指在一个有容积限制(或者重量限制)的背包中放入物品,物品拥有体积、重量和价值等属性。需要求一种满足背包限制的放置物品的方式,使得背包中物品的价值之和最大。根据物品的限制条件可分为01背包、完全背包、多重背包和分组背包等问题。背包问题是动态规划的经典问题之一,在实际中往往有很多变形,需要通过一些方法,把问题转化为背包问题。

完全背包

完全背包的特点是每种物品数量没有限制,可以无限使用。完全背包问题与01背包的解法相似,状态定义也一致。
子问题定义:F[i][j]表示前i种物品中选取若干件物品放入剩余空间为j的背包中所能得到的最大价值
递推关系:

f[i][j]=max(f[i-1][j-k*c[i]]+k*w[i])  0<=k*c[i]<=j
//与01背包类似 多一个数量k的枚举  k*c[i]不能超过现有容量

设物品种数为N,背包容量为V,第i种物品体积为C[i],第i种物品价值为W[i]。总的时间复杂度即为 O( N * V * ∑(j/C[i]) ) ,空间复杂度为O(VN)


一个可能的优化:
若两件物品满足C[i] ≤C[j]&&W[i] ≥W[j]时将第j种物品直接筛选掉。因为第i种物品比第j种物品物美价廉,用i替换j得到至少不会更差的方案(贪心)。
这个筛选过程如下:先找出体积大于背包的物品直接筛掉一部分(也可能一种都筛不掉)复杂度O(N)。利用计数排序思想对剩下的物品体积进行排序,同时筛选出同体积且价值最大的物品留下,其余的都筛掉(这也可能一件都筛不掉)复杂度O(V)。整个过程时间复杂度为O(N+V)


转化为01背包:
因为同种物品可以多次选取,那么第i种物品最多可以选取V/C[i]件价值不变的物品,然后就转化为01背包问题。整个过程的时间复杂度并未减少。如果把第i种物品拆成体积为C[i]× 2k 价值W[i]× 2k 的物品,其中满足C[i]× 2k ≤V。那么在求状态F[i][j]时复杂度就变为O(log2(V/C[i]))。整个时间复杂度就变为O(NVlog2(V/C[i]))


换一种递推思路,可以将时间复杂度变为O(NV)

for (int i = 1;i <= N;i++)  
{  
    for (int v = weight[i];v <= V;v++) //注意和01背包的差别 
    {  
        f[v] = max(f[v],f[v - weight[i]] + Value[i]);//节约空间写法  
    }  
}  
//这和01背包的伪代码很相似,在01背包的代码中,v变化的区间是逆序循环的,即[V,Weight[i]]。而这里,v变化的区间是顺序循环的,即为[Weight[i],V]。

解释:
再次给出定义:

f[i][v]表示把前i件物品放入容量为v的背包时的最大代价。

f[i-1][v-c[i]]表示把前i - 1件物品放入容量为v的背包时的最大代价.


f[i][v] = max(f[i - 1][v],f[i - 1][v - weight[i]] + Value[i]) //01背包  

01背包中,v变化的区间是逆序循环的原因:要保证由状态f[i-1][v-c[i]]递推状态f[i][v]时,f[i-1][v-c[i]]没有放入第i件物品。之后,在第i循环时,放入一件第i件物品。


f[i][v] = max(f[i - 1][v],f[i][v - weight[i]] + Value[i]);  //完全背包

完全背包中,v变化的区间是顺序循环的原因:完全背包的特点是每种物品可选无限件,在求解加选第i种物品带来的收益f[i][v]时,在状态f[i][v-c[i]]中已经尽可能多的放入物品i了,此时在f[i][v-c[i]]的基础上,我们可以再次放入一件物品i,此时也是在不超过背包容量的基础下,尽可能多的放入物品i。


例题:HDU 1114

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

Sample Input

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

Sample Output

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.


思路

定义状态dp[i][j]表示前i种面值的硬币组合后,得到重量之和为j时能得到的面值总和的最小值。

转移方程:dp[i][j]=min(dp[i-1][j-n*w[i]]+n*p[i])     0<=n * w[i]<=j
改进: dp[j]=min(dp[j],dp[j-w[i]]+p[i])   j>=w[i]


代码示例

#include<bits/stdc++.h>
using namespace std;

const int maxn=10005;

int dp[maxn];

int main()
{
    ios::sync_with_stdio(false);
    int n,t,e,f,cost,value,v;
    cin>>t;
    while(t--)
    {
        cin>>e>>f;
        v=f-e;
        cin>>n;
        memset(dp,-1,sizeof(dp));//初始化为-1,-1表示未到达
        dp[0]=0;
        for(int i=0;i<n;++i){
            cin>>value>>cost;
            for(int i=0;i+cost<=v;++i){
                if(dp[i]==-1) continue;//状态未达到,不能进行转移
                else if(dp[i+cost]==-1) dp[i+cost]=dp[i]+value;//如果第一次到达该状态,直接赋值
                else dp[i+cost]=min(dp[i+cost],dp[i]+value);//注意递推式与max的不同
            }
        }
        if(dp[v]==-1) printf("This is impossible.\n");
        else printf("The minimum amount of money in the piggy-bank is %d.\n",dp[v]);

    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值