您的位置:首页 > 其它

多重背包问题 POJ 2392

2014-04-16 23:42 295 查看
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>

using namespace std;

struct block {
int height;
int maxHeight;
int number;
bool operator < (const block& b)const {
if(maxHeight < b.maxHeight) return true;
return false;
}
};

block bList[410];
int numList[410][40010];
int mHeight = 0;
int realHeight = 0;

void completePack(int type,int length)
{
for (int i=length; i<=bList[type].maxHeight; i++) {
numList[type][i] = max(numList[type][i],numList[type][i-length]+length);
if(realHeight < numList[type][i]) realHeight = numList[type][i];
}
}

void zeroOnePack(int type,int length,int blockNumber)
{
for (int i=bList[type].maxHeight; i>=length*blockNumber; i--) {
numList[type][i] = max(numList[type][i],numList[type][i-length*blockNumber]+length*blockNumber);
if(realHeight < numList[type][i]) realHeight = numList[type][i];
}
}

void multiplePack(int type,int length,int maxHeight,int blockNumber)
{
for (int i=1; i<=mHeight; i++) numList[type][i] = numList[type-1][i];
if(length * blockNumber >= mHeight) {
completePack(type,length);
}
int k = 1;
while(k < blockNumber) {
zeroOnePack(type,length,k);
blockNumber -= k;
k = k * 2;
}
zeroOnePack(type,length,blockNumber);
}

int main()
{
int blockNum = 0;
while(~scanf("%d",&blockNum)) {
memset(numList,0,sizeof(numList));
for (int i=1; i<=blockNum; i++) scanf("%d%d%d",&bList[i].height,&bList[i].maxHeight,&bList[i].number);
sort(bList+1,bList+1+blockNum);
mHeight = bList[blockNum].maxHeight;
for (int i=1; i<=blockNum; i++) multiplePack(i,bList[i].height,bList[i].maxHeight,bList[i].number);
printf("%d\n",realHeight);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  poj DP