多重背包,多重背包可以转化为完全背包和01背包。如果某东西的总体积大于包体积,则可以当成是完全背包。否则按物品体积的1,2,4...倍分别进行01背包。这样就相当于构成了所有的可能情况,例如5 = 4 + 1, 7 = 1 + 2 + 4。都可以由这些数字构成。
View Code
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;
#define maxn 405
#define maxm 40005
struct Block
{
int count, h, limit;
}block[maxn];
int n;
bool f[maxm];
bool operator < (const Block &a, const Block &b)
{
return a.limit < b.limit;
}
void compackage(int h, int limit)
{
for (int i = h; i <=limit; i++)
f[i] = f[i] || f[i - h];
}
void zerpackage(int h, int limit)
{
for (int i = limit; i >=h; i--)
f[i] = f[i] || f[i - h];
}
void mulpackage(int count, int h, int limit)
{
if (h * count >= limit)
{
compackage(h, limit);
return;
}
int i = 1;
while (count >= i)
{
zerpackage(h * i, limit);
count -= i;
i <<= 1;
}
zerpackage(h * count, limit);
}
int main()
{
//freopen("t.txt", "r", stdin);
scanf("%d", &n);
for (int i = 0; i < n; i++)
scanf("%d%d%d", &block[i].h, &block[i].limit, &block[i].count);
sort(block, block + n);
memset(f, 0, sizeof(f));
f[0] = true;
for (int i = 0; i < n; i++)
mulpackage(block[i].count, block[i].h, block[i].limit);
for (int i = block[n - 1].limit; i >= 0; i--)
if (f[i])
{
printf("%d\n", i);
break;
}
return 0;
}