标签:
先跑一遍01背包,再跑一遍多重背包
1 100 2 10 2 1 20 1 1
21HintCRB‘s mom buys 10 presents of first kind, and receives 2 × 10 + 1 = 21 candies.
/* *********************************************** Author :CKboss Created Time :2015年08月21日 星期五 13时29分32秒 File Name :HDOJ5410.cpp ************************************************ */ #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <string> #include <cmath> #include <cstdlib> #include <vector> #include <queue> #include <set> #include <map> using namespace std; const int maxn=1100; int m,n; int w[maxn],A[maxn],B[maxn]; int dp[maxn*2]; int main() { //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); int T_T; scanf("%d",&T_T); while(T_T--) { scanf("%d%d",&m,&n); for(int i=1;i<=n;i++) scanf("%d%d%d",w+i,A+i,B+i); memset(dp,0,sizeof(dp)); for(int i=1;i<=n;i++) { for(int j=m;j>=w[i];j--) { dp[j]=max(dp[j],dp[j-w[i]]+A[i]+B[i]); } } for(int i=1;i<=n;i++) { for(int j=w[i];j<=m;j++) { dp[j]=max(dp[j],dp[j-w[i]]+A[i]); } } printf("%d\n",dp[m]); } return 0; }
版权声明:来自: 码代码的猿猿的AC之路 http://blog.csdn.net/ck_boss
HDOJ 5410 CRB and His Birthday DP背包
标签:
原文地址:http://blog.csdn.net/ck_boss/article/details/47833733