标签:color using else ... 01背包 des 基础 printf logs
10 3 2 1 0 3 3 1 4 5 4
11
【思路】
其实就是将三种背包合在了一起,加个if判断是怎么背包 再怎样做就可以了
//
01背包是最基础的背包 容量V 价值 重量 求V范围内价值最大的
多重背包是在01背包的基础上 只能取1件改成了能取多件
完全背包则是取无限件
01背包 和 多重背包是很像的
注意如果 容量v逆序是01和多重 这样不会重复拿取产生矛盾
顺序则是 完全背包了
【代码】
#include<iostream> #include<cstdio> using namespace std; int w[20],v[20],f[20],cnt[20]; int main() { int m,n; scanf("%d%d",&m,&n); for(int i=1;i<=n;i++) scanf("%d%d%d",&w[i],&v[i],&cnt[i]); for(int i=1;i<=n;i++) { if(cnt[i]==0)//如果是完全背包 { for(int j=w[i];j<=m;j++) { f[j]=max(f[j],f[j-w[i]]+v[i]); } } else { for(int k=1;k<=cnt[i];k++) { for(int l=m;l>=w[i];l--) { f[l]=max(f[l],f[l-w[i]]+v[i]); } } } } printf("%d",f[m]); return 0; }
标签:color using else ... 01背包 des 基础 printf logs
原文地址:http://www.cnblogs.com/zzyh/p/6741123.html