2015 summer vacation multi-School Cooperation --- CRB and His Birthday (01 backpack), 2015 --- crb

Source: Internet
Author: User

2015 summer vacation multi-School Cooperation --- CRB and His Birthday (01 backpack), 2015 --- crb

Question Link

Http://acm.split.hdu.edu.cn/showproblem.php? Pid = 1, 5410

 

Problem DescriptionToday is CRB's birthday. His mom decided to buy too 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 ≤ MB ≤ 2000
1 ≤ N ≤1000
0 = Ai, Bi = 2000
1 ≤ Wi ≤2000
  InputThere 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.
  OutputFor each test case, output the maximum candies she can gain. Sample Input1100 210 2 120 1 1 Sample Output21 HintCRB's mom buys 10 presents of first kind, and between ES 2 × 10 + 1 = 21 candies. AuthorKUT (DPRK) Source2015 Multi-University Training Contest 10 RecommendIn wange2014, enter M and N, respectively, indicating the total amount of money and the number of items. Then, enter N rows, with 3 numbers in each row, the unit price, the quantity of sugar to be bought, and the quantity of sugar to be bought at a time. How much sugar can be obtained at most? Idea: 01 backpack, dp [I] indicates the maximum amount of sugar that can be obtained under I, vis [I] [j] indicates the maximum amount of sugar that can be obtained under I, whether to buy j items, state transition equation dp [I] = dp [I-kind [j] [0] + kind [j] [1] + (vis [I-kind [j] [0]] [j] = 0) * kind [j] [2]; the Code is as follows:
#include <iostream>#include <algorithm>#include <cstdio>#include <cstring>#include <cmath>#define eps 1e-8#define maxn 105#define inf 0x3f3f3f3f3f3f3f3f#define IN freopen("in.txt","r",stdin);using namespace std;int dp[2005];int vis[2005][1005];int kind[1005][3];int main(){    int T;    int M,N;    cin>>T;    while(T--)    {        scanf("%d%d",&M,&N);        for(int i=0;i<N;i++)            scanf("%d%d%d",&kind[i][0],&kind[i][1],&kind[i][2]);        memset(dp,0,sizeof(dp));        memset(vis,0,sizeof(vis));        for(int i=1;i<=M;i++)        {            int flag=-1;            for(int j=0;j<N;j++)            {                if(i<kind[j][0]) continue;                int s=dp[i-kind[j][0]]+kind[j][1];                if(!vis[i-kind[j][0]][j]) s+=kind[j][2];                if(dp[i]<s)                {                    dp[i]=s;                    flag=j;                }            }            if(flag>=0)            {                for(int j=0;j<N;j++)                {                    vis[i][j]=vis[i-kind[flag][0]][j];                }                vis[i][flag]++;            }        }        int tmp=0;        for(int i=1;i<=M;i++)           tmp=max(tmp,dp[i]);        printf("%d\n",tmp);    }    return 0;}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.