01背包+完全背包 HDU - 5410 

喜欢ヅ旅行 2022-06-17 09:27 306阅读 0赞

Today is CRB’s birthday. His mom decided to buy many presents for her lovely son.
She went to the nearest shop with M
M
Won(currency unit).
At the shop, there are N
N
kinds of presents.
It costs Wi
Wi
Won to buy one present of i
i
-th kind. (So it costs k
k
× Wi
Wi
Won to buy k
k
of them.)
But as the counter of the shop is her friend, the counter will give Ai × x + Bi
Ai × x + Bi
candies if she buys x
x
(x
x

0) presents of i
i
-th kind.
She wants to receive maximum candies. Your task is to help her.
1 ≤ T
T
≤ 20
1 ≤ M
M
≤ 2000
1 ≤ N
N
≤ 1000
0 ≤ Ai, Bi
Ai, Bi
≤ 2000
1 ≤ Wi
Wi
≤ 2000

Input

There are multiple test cases. The first line of input contains an integer T
T
, indicating the number of test cases. For each test case:
The first line contains two integers M
M
and N
N
.
Then N
N
lines follow, i
i
-th line contains three space separated integers Wi
Wi
, Ai
Ai
and Bi
Bi
.

Output

For each test case, output the maximum candies she can gain.

Sample Input

1
100 2
10 2 1
20 1 1

Sample Output

21

Hint

CRB’s mom buys 10 presents of first kind, and receives 2 × 10 + 1 = 21 candies.

  1. #include <iostream>
  2. #include <cstring>
  3. #include <cstdio>
  4. #include <string>
  5. #include <map>
  6. #include <queue>
  7. #include <set>
  8. #include <algorithm>
  9. #define LL long long
  10. #define MAX 2005
  11. using namespace std;
  12. int dp[MAX];
  13. int w[MAX],a[MAX],b[MAX];
  14. int t,m,n;
  15. int main()
  16. {
  17. scanf("%d",&t);
  18. while(t--)
  19. {
  20. scanf("%d%d",&m,&n);
  21. for(int i=1;i<=n;i++)
  22. scanf("%d%d%d",&w[i],&a[i],&b[i]);
  23. memset(dp,0,sizeof(dp));
  24. for(int i=1;i<=n;i++)
  25. for(int j=m;j>=w[i];j--)
  26. {
  27. dp[j]=max(dp[j],dp[j-w[i]]+a[i]+b[i]); //先通过01背包来确定拿不拿这件商品的最大价值
  28. }
  29. for(int i=1;i<=n;i++)
  30. for(int j=w[i];j<=m;j++)
  31. {
  32. dp[j]=max(dp[j],dp[j-w[i]]+a[i]); //用完全背包计算拿了x件商品的最大价值(a[i]*x+b[i])
  33. }
  34. printf("%d\n",dp[m]);
  35. }
  36. return 0;
  37. }
  38. /*题意:
  39. 有M多的钱 去买n种商品
  40. 每种商品的价格为w 如果你买x个这种商品 你会得到 a*x+b个糖果
  41. 问最多能得到多少糖果*/
  42. /*思路:
  43. 对于每件商品 你如果买一件 那么你会得到a+b个糖果
  44. 如果再次基础上每一次的购买你就只能得到a个糖果
  45. 所以可以看所这种商品有两种价值
  46. 第一种价值只有一次 用01背包解决
  47. 第二种价值可以无限次购买 用完全背包解决 */

发表评论

表情:
评论列表 (有 0 条评论,306人围观)

还没有评论,来说两句吧...

相关阅读

    相关 01背包,完全背包

    01背包问题:一个背包总容量为V,现在有N个物品,第i个 物品体积为weight\[i\],价值为value\[i\],现在往背包里面装东西,怎么装能使背包的内物品价值最大?