逃亡的准备(hallows)
【问题描述】
在《Harry Potter and the Deathly Hallows》中,Harry Potter他们一起逃亡,现在有许多的东西要放到赫敏的包里面,但是包的大小有限,所以我们只能够在里面放入非常重要的物品,现在给出该种物品的数量、体积、价值的数值,希望你能够算出怎样能使背包的价值最大的组合方式,并且输出这个数值,赫敏会非常地感谢你。
【输入格式】
第一行有2个整数,物品种数n和背包装载体积v;
第二行到i+1行每行3个整数,为第i种物品的数量m、体积w、价值s。
【输出格式】
仅包含一个整数,即为能拿到的最大的物品价值总和。
【输入样例】
2 10
3 4 3
2 2 5
【输出样例】
13
样例说明:选第一种一个,第二种两个。结果为3×1+5×2=13
【数据规模】
对于30%的数据:1≤v≤500;1≤n≤2000;1≤m≤10;1≤w≤20;1≤s≤100;
对于100%的数据:1≤v≤500;1≤n≤2000;1≤m≤5000;1≤w≤20;1≤s≤100。
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
using namespace std;
struct bag
{
int w;
int p;
};
bag s[1000005];
int f[8005];
int n, v;
int tot=0;
void solve()
{
f[0]=0;
for (int i=1; i<=tot; i++)
for (int j=v; j>=s[i].w; j--)
f[j]=max(f[j-s[i].w]+s[i].p, f[j]);
printf("%d", f[v]);
}
void chai(int m, int w, int p)
{
s[++tot].w=w;
s[tot].p=p;
m-=1;
int k=1;
while (k*2<m)
{
k=k*2;
s[++tot].w=k*w;
s[tot].p=k*p;
m-=k;
}
s[++tot].w=m*w;
s[tot].p=m*p;
}
void init()
{
memset(s, 0, sizeof(s));
memset(f, 0, sizeof(f));
scanf("%d %d", &n, &v);
for (int i=1; i<=n; i++)
{
int m , w, p;
scanf("%d %d %d", &m, &w, &p);
{
chai(m, w, p);
}
}
}
int main()
{
freopen("hallows.in", "r", stdin);
freopen("hallows.out", "w", stdout);
init();
solve();
return 0;
}