HDU 5410 CRB and His Birthday

CRB and His Birthday

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 341    Accepted Submission(s): 181


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
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.
 
Author
KUT(DPRK)
 
DP  背包。
思路:看上去像完全背包,只不过多出个bi,不太好弄。依据完全背包的思想,我们可以把每类物品分成1,2,4,8.....2^k份。然后进行01背包。其中注意一点,要将bi放在每一类的前面。这样才能保证答案的正确性 代码:
/* ***********************************************
Author        :PK29
Created Time  :2015/8/20 20:09:35
File Name     :4.cpp
************************************************ */
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <iomanip>
#include <list>
#include <deque>
#include <stack>
#define ull unsigned long long
#define ll long long
#define mod 90001
#define INF 0x3f3f3f3f
#define maxn 10000+10
#define cle(a) memset(a,0,sizeof(a))
const ull inf = 1LL << 61;
const double eps=1e-5;
using namespace std;

bool cmp(int a,int b){
    return a>b;
}
int n,m;
int A[100004],B[100004],dp[100004];
int main()
{
    #ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
    #endif
    //freopen("out.txt","w",stdout);
    int T,w,a,b;
    cin>>T;
    while(T--){
        scanf("%d%d",&m,&n);
        int j=0;
        for(int i=1;i<=n;i++){
            scanf("%d%d%d",&w,&a,&b);
            j++,A[j]=a+b,B[j]=w;
            while(w<=m){
                j++,A[j]=a,B[j]=w,a*=2,w*=2;
            }
        }
        cle(dp);
        for(int i=1;i<=j;i++)
            for(int k=m;k>=B[i];k--)
                dp[k]=max(dp[k],dp[k-B[i]]+A[i]);
        printf("%d
",dp[m]);
    }    
    return 0;
}
价值        花费         个数
A[1]=3   B[1]=10      1
A[2]=2   B[2]=10      1
A[3]=4   B[3]=20      2
A[4]=8   B[4]=40      4
A[5]=16 B[5]=80      8
A[6]=2   B[6]=20      1
A[7]=1   B[7]=20      1 
A[8]=2   B[8]=40      2
A[9]=4   B[9]=80      4
原文地址:https://www.cnblogs.com/pk28/p/4748019.html