完全背包问题的特点是每种物品数量没有限制,可以无限使用。很容易定义完全背包状态转移方程,根据第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:
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!
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.
<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)的算法。改进后状态转移方程为:
<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>