完全背包问题

完全背包问题的特点是每种物品数量没有限制,可以无限使用。很容易定义完全背包状态转移方程,根据第i件物品进行划分进行决策。设第i个物品选择k个,于是:

            W[i,j]=max{ W[i -1,j - k*v[ i ] ]+k*p[i] }   (0<= k<=j / v[i] ),k=0时即表示不用选择的情况。此时对于每一个状态i,我们要考虑(1+2+..V)/v[i]次。当v[i]比较小时,光考虑第i个状态时间复杂度就接近O(V^2),总时间复杂度接近O(NV^2).当然还可以对其做一些改进。为了方便操作,将价格和体积捆绑为一个结构体Goods,两个属性是p和v。状态转移方程可以写为:

            W[i,j]=max{ W[i -1,j - k*goods[i].v+k*goods[i].p }   (0<= k<=j / v[i] ) .

      对于不要求背包装满的情况,首先可以做一些简单的优化,对于第i个物品和第j个物品,若满足 v[i]<=v[j]且p[i]>=p[j]时,我们完全可以不考虑第j个物品,这是由于物品i相对于物品j来说是物美价廉。我们可以用桶排序方法来完成优化。建立bucket[1...V],超过体积V的物品直接删除。当没有体积为 i 的物品时,bucket[i]设置为空。当有多个体积为i的物品时,bucket[i]中放体积为i但是价格最大的那个物品。然后扫描一次桶,若i<j但是bucket[i].p>=bucket[j].p则将bucket[j]设置为空(删掉)。总的过程时间为o(V).,空间也为O(V).但是对于要求将背包装满的情况则不能这样优化,否则本来有解也可能导致无解。当然这些优化并不能改变最坏情况下的复杂度。下面一种思想将完全背包问题转变为01背包问题,对于第 i 个物品,最多有k=V/v[i]个.从而我们可以将第i个物品分解为k个一样的物品。这样问题就转化为了01背包问题,然而时间复杂度没变。另一个改进是采用二进制的思想:将第i个物品质分解为体积:v[i] , 2*v[i],v[i]*2^2...v[i]*2^log(V/v[i]),相应价值也类似递增,这是由于二进制可以表示任意数。这样得到了一个不错的优化结果,第i个物品分解为log(V/v[i])+1个,第 i 个状态转移次数大约为(log(V/v[i])+1)*V,当v[i]比较小时,此式子接近O(VlogV),于是总的时间度接近O(NVlogV),已经得到了一个很大的改进。以下代码是不要背包求装满的情况,开始部分利用桶排序原则删除了一些物品,然后将物品分解转变为二进制。

<span style="font-size:10px;">/*不要求背包装满*/</span>
<span style="font-size:10px;">#include<iostream>
using namespace std;
#define MAX_SIZE 500
int N, V;
struct Goods{
	int p, v;
};
Goods goods[MAX_SIZE];
Goods bucket[MAX_SIZE];
bool flag[MAX_SIZE];
int W[MAX_SIZE];
int main(){
	int i, j,pos,nowPrice,ans,total;
	Goods temp;
	memset(flag, 0, sizeof(flag));
	cin >> N >> V;
	for (i = 1; i <=N; i++)
		cin >> goods[i].v >> goods[i].p;
	//-------------------------------------------------------------------------
	for (i = 1; i <=N; i++){
		if (goods[i].v>V)
			continue;
		else  if (!flag[goods[i].v] || goods[i].p>bucket[goods[i].v].p){
			flag[goods[i].v] = 1;
			bucket[goods[i].v] = goods[i];
		}
	}
	pos =1,ans=1;
	while (!flag[pos])
		pos++;
	nowPrice = bucket[pos].p;
	goods[ans++] = bucket[pos];
	for (i = pos + 1; i <=V; i++){
		if (flag[i] && nowPrice < bucket[i].p){
			nowPrice = bucket[i].p;
			goods[ans++] = bucket[i];
		}
	}

	/*--------------------------------------------------------------以上是优化过程代码*/
	pos = ans;
	for (i = 1; i < ans; i++){
		temp = goods[i];</span>
<span style="font-size:10px;">                total=temp.v;
		while(total<V){
			temp.v *= 2;
			temp.p *= 2;
			goods[pos++] = temp;</span>
<span style="font-size:10px;">                        total+=temp.v;
		}
	}
	/*--------------------------以上完成拆分,如今已有pos个物品,下面利用01背包解法*/
	memset(W, 0, sizeof(W));
	for (i = 1; i <= pos; i++){
		for (j = V; j >= goods[i].v; j--)
		if (W[j]< W[j - goods[i].v] + goods[i].p)
			W[j] = W[j - goods[i].v] + goods[i].p;
	}
	cout << W[V] << endl;
	return 0;
}</span>

在要求背包装满的情况下,就不得做删除的优化了。HDU1004就是一个要求背包装满的问题。下面是HDU1004:

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

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.
 
这是一个要求典型的要求背包装满的完全背包问题。直接给出二进制分解后的AC代码:
<span style="font-size:10px;">#include<iostream>
using namespace std;
#define MAX_SIZE 10002
int N, V;
struct Goods{
	int p, v;
};
Goods goods[MAX_SIZE];
int W[MAX_SIZE];
int main(){
	int i, j,pos,ans,E,F,T,total;
	Goods temp;
	cin >> T;
	while (T--){
		memset(W, -1, sizeof(W));
		W[0] = 0;
		cin >> E >> F;
		V = F - E;
		cin >> N;
		for (i = 1; i <= N; i++)
			cin >>goods[i].p>>goods[i].v;
		ans = N + 1;
		for (i = 1; i <=N; i++){
			temp = goods[i];
			total = temp.v;
			while (total <V){
				temp.v *= 2;
				temp.p *= 2;
				goods[ans++] = temp;
				total += temp.v;
			}
		}
		for (i = 1; i <ans; i++){
			for (j = V; j >= goods[i].v; j--){
				pos = j - goods[i].v;
				if (W[pos] != -1 && (W[j] == -1 || W[pos] + goods[i].p < W[j]))
					W[j] = W[pos] + goods[i].p;
			}
		}
		if (W[V] != -1)
			cout << "The minimum amount of money in the piggy-bank is " << W[V] << '.' << endl;
		else
			cout << "This is impossible." << endl;
	}
	return 0;
}</span>

二进制分解基于思想是将完全背包问题转化为01背包问题。但是这种方法也不是完全背包最快的办法,最后还有时间复杂度为O(NV)的算法。进一步改进,并让 j 枚举 0..V的情况,有O(VN)的算法。改进后状态转移方程为:
                    W[ j ]=MAX{W[ j ],W[ j-v[ i] ] +p[ i ]};
在01背包问题中,我们只关心第i中物品选还是不选。而完全背包是每件物品最多可选V/v[i]件。所以在考虑加选一种第i中物品时前面在子结果W[ i,j-v[i]]中可能已经选过第i件物品。即是要采用 for j=v[i] to V的循环,同样的求解HDU 1114 时,使用时间比二进制方法快很多:
<span style="font-size:10px;">#include<iostream>
using namespace std;
#define MAX_SIZE 10002
int N, V;
struct Goods{
	int p, v;
};
Goods goods[MAX_SIZE];
int W[MAX_SIZE];
int main(){
	int i, j,pos,E,F,T;
	cin >> T;
	while (T--){
		memset(W, -1, sizeof(W));
		W[0] = 0;
		cin >> E >> F;
		V = F - E;
		cin >> N;
		for (i = 1; i <= N; i++)
			cin >>goods[i].p>>goods[i].v;
		for (i = 1; i <=N; i++){
			for (j = goods[i].v; j <= V; j++){
				pos = j-goods[i].v;
				if (W[pos] != -1 && (W[j] == -1 || W[pos] + goods[i].p < W[j]))
					W[j] = W[pos] + goods[i].p;
			}
		}
		if (W[V] != -1)
			cout << "The minimum amount of money in the piggy-bank is " << W[V] << '.' << endl;
		else
			cout << "This is impossible." << endl;
	}
	return 0;
}</span>



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值