2015多校联合第十场hdu5410CRB and His Birthday 01背包+完全背包

来源:互联网 发布:设置铃声的软件 编辑:程序博客网 时间:2024/05/22 06:35

Problem Description
Today is CRB's birthday. His mom decided to buy many presents for her lovely son.
She went to the nearest shop with M Won(currency unit).
At the shop, there are N kinds of presents.
It costs Wi Won to buy one present of i-th kind. (So it costs k × Wi Won to buy k of them.)
But as the counter of the shop is her friend, the counter will give Ai × x + Bi candies if she buys x(x>0) presents of i-th kind.
She wants to receive maximum candies. Your task is to help her.
1 ≤ T ≤ 20
1 ≤ M ≤ 2000
1 ≤ N ≤ 1000
0 ≤ Ai, Bi ≤ 2000
1 ≤ Wi ≤ 2000
 

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

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

Sample Input
1100 210 2 120 1 1
 

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

第一次搜多校赛的题解,好在于看题解明白了啊啊啊,好激动,而且背着敲1A!!多久没有了这种感觉了T^T

说正经的-->_--> 这题啥意思 ?完全背包中若选择某种物品,另加b[i]的价值,只加一次

开始百思不得其姐啊啊啊,怎么记录某种物品买没买啊??再加数组太麻烦,把自己绕糊涂了orz

后来题解说先进行一次01背包再进行一次多重背包 茅塞顿开啊啊啊啊

01那次加b[i] 多重背包加只a[i] 01就相当于第一次取某种物品 多重背包 理论知识详见我上一篇博客

刚刚想到会不会两个for重复矛盾了了呢?不不不 装第一个的时候 下面完全背包的总价值一定比01背包的小 Hhhhhh

贴上我光辉的代码 怪不得过的人那么多 看了题解谁都会-->_-->

#include <iostream>#include<cstring>#include<cstdio>using namespace std;int dp[2005],cost[2005],a[2005],b[2005];int main(){    //freopen("cin.txt","r",stdin);    int t,m,n;    while(cin>>t)    {        while(t--)        {            cin>>m>>n;            memset(dp,0,sizeof(dp));            for(int i=0;i<n;i++) cin>>cost[i]>>a[i]>>b[i];            for(int i=0;i<n;i++)            {                for(int j=m;j>=cost[i];j--)                    if(dp[j]<dp[j-cost[i]]+a[i]+b[i]) dp[j]=dp[j-cost[i]]+a[i]+b[i];                for(int j=cost[i];j<=m;j++)                    if(dp[j]<dp[j-cost[i]]+a[i]) dp[j]=dp[j-cost[i]]+a[i];            }            cout<<dp[m]<<endl;        }    }    return 0;}



0 0