题意:裸的多重背包
想我第一次过这道题的时候还是在(2017-03-28 20:08:52),如今都快退役了,连多重背包都写不好,晚上补了焦作网络赛的H,发现之前的多重背包T了,然后了解了一下二进制优化,其实这个优化还是挺巧的,在背包九讲中还介绍了单调队列的做法,单调队列的做法复杂度更为优秀,在此贴出传送门,我觉得这和写的很棒,起码多重背包的解法都具备了
然后就是愉快的搬运代码时间
#include <bits/stdc++.h> using namespace std; const int maxn=205; int p[maxn],w[maxn],b[maxn]; int dp[maxn]; int main() { int T; scanf("%d",&T); while(T--){ int m,n; scanf("%d%d",&n,&m); memset(dp,0,sizeof(dp)); for(int i=1;i<=m;i++){ scanf("%d%d%d",&p[i],&w[i],&b[i]); } for(int i=1;i<=m;i++){ int now=b[i]; for(int j=1;j<=now;j<<=1){ for(int k=n;k>=j*p[i];k--){ dp[k]=max(dp[k],dp[k-j*p[i]]+j*w[i]); } now-=j; } for(int k=n;k>=now*p[i];k--){ dp[k]=max(dp[k],dp[k-now*p[i]]+now*w[i]); } } printf("%d ",dp[n]); } return 0; }