您的位置:首页 > 其它

poj 2392 Space Elevator(多重背包)

2017-08-15 22:17 381 查看
先排序,再背包。

可转成完全背包

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;

const int MAXN = 500;
int dp[MAXN*100];
struct block
{
int h,a,c;
};
block bs[MAXN];

bool cmp(const block& a, const block& b)
{
return a.a < b.a;
}

void ZeroOnePack(int w, int v, int W)
{
for(int j = W; j >= w; --j)
dp[j] = max(dp[j],dp[j-w]+v);
}

int main()
{
int k,sum = 0;;
scanf("%d",&k);
for(int i = 0; i < k; ++i)
scanf("%d %d %d",&bs[i].h,&bs[i].a,&bs[i].c);
sort(bs,bs+k,cmp);
for(int i = 0; i < k; ++i)
{
if(bs[i].h*bs[i].c >= bs[i].a)
{
for(int j = bs[i].h; j <= bs[i].a; ++j)
dp[j] = max(dp[j-bs[i].h]+bs[i].h,dp[j]);
}
else
{
int temp = bs[i].c;
for(int k = 1; k <= temp; k *=2)
{
ZeroOnePack(k*bs[i].h,k*bs[i].h,bs[i].a);
temp -= k;
}
if(temp) ZeroOnePack(temp*bs[i].h,temp*bs[i].h,bs[i].a);
}
}
int res = 0;
for(int i = bs[k-1].a; i >= 0; --i)
res = max(res,dp[i]);
printf("%d\n",res);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: