51nod-【多重背包问题】
2016-08-20 16:48
218 查看
<span style="font-size:18px;">#include<cstdio>
#include<cstring>
#define LL long long
LL f[50000+11];
struct node
{
int a;//重量
int b;//价值
}arr[50000];
LL max(LL x,LL y)
{
if(x>y)
return x;
return y;
}
int main()
{
LL n,w;
scanf("%lld%lld",&n,&w);
memset(f,0,sizeof(f));
LL wi,pi,ci,i,j=1;
for(i=1;i<=n;++i)
{
scanf("%lld%lld%lld",&wi,&pi,&ci);
int temp=1;
while(ci>=temp)
{
arr[j].a=wi*temp;//重量
arr[j].b=pi*temp;//价值
++j;
ci-=temp;
temp*=2;
}
arr[j].a=wi*ci;
arr[j].b=pi*ci;
++j;//这个地方一定要注意,j++,是不能少的,因为while循环可能进不去j的值一直等于1
} //开始没有加,找了好长时间都没有发现错误
for(i=1;i<j;++i)
{
for(int k=w;k>=arr[i].a;--k)
f[k]=max(f[k],f[k-arr[i].a]+arr[i].b);
}
printf("%lld\n",f[w]);
return 0;
}</span>
#include<cstring>
#define LL long long
LL f[50000+11];
struct node
{
int a;//重量
int b;//价值
}arr[50000];
LL max(LL x,LL y)
{
if(x>y)
return x;
return y;
}
int main()
{
LL n,w;
scanf("%lld%lld",&n,&w);
memset(f,0,sizeof(f));
LL wi,pi,ci,i,j=1;
for(i=1;i<=n;++i)
{
scanf("%lld%lld%lld",&wi,&pi,&ci);
int temp=1;
while(ci>=temp)
{
arr[j].a=wi*temp;//重量
arr[j].b=pi*temp;//价值
++j;
ci-=temp;
temp*=2;
}
arr[j].a=wi*ci;
arr[j].b=pi*ci;
++j;//这个地方一定要注意,j++,是不能少的,因为while循环可能进不去j的值一直等于1
} //开始没有加,找了好长时间都没有发现错误
for(i=1;i<j;++i)
{
for(int k=w;k>=arr[i].a;--k)
f[k]=max(f[k],f[k-arr[i].a]+arr[i].b);
}
printf("%lld\n",f[w]);
return 0;
}</span>
相关文章推荐
- 51nod 1007 正整数分组 (类01背包问题)
- 51Nod 1085 背包问题(01背包 )
- 51nod 1085 背包问题(01背包)
- 51nod 1085 背包问题
- 51nod 1086 背包问题 V2
- 51nod 多重背包问题 (dp)
- 51Nod-背包问题V2(多重背包+二进制优化)
- 51nod-1086 背包问题(多重背包)
- 51nod-0-1背包问题
- 51nod 1085 背包问题【01背包】
- 51Nod 1086 背包问题 V2(二进制多重背包)
- 51Nod 有限背包计数问题 题解报告
- 51nod 1086 背包问题V2(巧妙拆分多重背包)
- 51nod 1257 背包问题 V3(二分)
- 51nod 多重背包问题
- 51nod 1085 背包问题
- 51Nod-1085-背包问题
- 51nod 1086 背包问题 V2 多重背包
- 51NOD 背包问题v2(动态规划)
- 51Nod 1086 背包问题 V2(多重背包)