CRB and His Birthday
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1112 Accepted Submission(s): 559
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
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 Wi, Ai and Bi.
The first line contains two integers M and N.
Then N lines follow, i-th line contains three space separated integers Wi, Ai 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)
Source
题意:
买东西,给你N的钱M种类的东西
每类东西,如果买就送A*x+B的蜡烛。
x为购买数,问最多可以获得多少蜡烛。
思路:因为送A*x+B的蜡烛,如果没有后面的常数B,就相当于价值为A,所以直接完全背包就可以解决。
但是后面多了个常数B,所以还有考虑买不买的情况,所以01背包先跑一遍,再跑一遍完全背包就可以了。
O(M*N);
1 #include<stdio.h> 2 #include<algorithm> 3 #include<iostream> 4 #include<string.h> 5 #include<math.h> 6 #include<queue> 7 #include<string.h> 8 #include<stack> 9 #include<vector> 10 #include<map> 11 #define sc(x) scanf("%I64d",&x) 12 #define pr(x) printf("%I64d",x) 13 #define prr(x) printf("%I64d ",x) 14 #define prrr(x) printf(" %I64d",x) 15 #define FOR(i,p,q) for(int i=p;i<=q;i++) 16 typedef struct pp 17 { 18 int x; 19 int y; 20 int z; 21 }ss; 22 ss aa[3000]; 23 int dp[3000]; 24 using namespace std; 25 int main(void) 26 { 27 int n,i,j,k,p,q; 28 int N,M; 29 scanf("%d",&k); 30 while(k--) 31 { 32 scanf("%d %d",&N,&M); 33 memset(dp,0,sizeof(dp)); 34 for(i=1;i<=M;i++) 35 { 36 scanf("%d %d %d",&aa[i].x,&aa[i].y,&aa[i].z); 37 } 38 for(i=1;i<=M;i++) 39 { 40 for(j=N;j>=aa[i].x;j--) 41 { 42 dp[j]=max(dp[j],dp[j-aa[i].x]+aa[i].z+aa[i].y); 43 } 44 } 45 for(i=1;i<=M;i++) 46 { 47 for(j=aa[i].x;j<=N;j++) 48 { 49 dp[j]=max(dp[j],dp[j-aa[i].x]+aa[i].y); 50 } 51 } 52 printf("%d ",dp[N]); 53 } 54 return 0; 55 }