多重背包问题 POJ 2392

来源:互联网 发布:深圳蜂鸟软件科技 编辑:程序博客网 时间:2024/06/01 08:11
#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;}

0 0