AcWing 7. 混合背包问题
原题链接
中等
作者:
Tyouchie
,
2019-03-21 09:37:41
,
所有人可见
,
阅读 1812
#include<bits/stdc++.h>
using namespace std;
#define N 500100
template<typename T>inline void read(T &x)
{
x=0;T f=1,ch=getchar();
while(!isdigit(ch)) {if(ch=='-') f=-1; ch=getchar();}
while(isdigit(ch)) {x=x*10+ch-'0'; ch=getchar();}
x*=f;
}
int n,v,c[N],w[N],f[N],h=0,s[N];
int main()
{
read(n);read(v);
for(int i=1;i<=n;i++)
{
int x, y, z, t;
read(x), read(y), read(z);
if(z < 0)
{
c[++h] = x, w[h] = y, s[h] = 1;
}
else if (z == 0) {
c[++h] = x, w[h] = y, s[h] = 0;
}
else if (z > 0) {
t = 1;
while (t <= z) {
c[++h] = t * x;
w[h] = t * y;
s[h] = 1;
z -= t;
t <<= 1;
}
c[++h] = z * x;
w[h] = z * y;
s[h] = 1;
}
}
f[0] = 0;
for (int i = 1; i <= h; ++i) {
if (s[i] == 1) {
for (int j = v; j >= c[i]; --j)
f[j] = max(f[j], f[j - c[i]] + w[i]);
}
else {
for (int j = c[i]; j <= v; ++j)
f[j] = max(f[j], f[j - c[i]] + w[i]);
}
}
printf("%d\n", f[v]);
return 0;
}