Nine degrees OJ topic 1455: Cherish now, Thanksgiving life

Source: Internet
Author: User

Title Description:

In order to save the lives of compatriots in the disaster area, you are prepared to purchase some food support disaster areas, now suppose you have a total of funds N, and the market has m rice, each rice is bagged products, its price range, and can only buy the whole bag. Excuse me: How many kilograms of grain can you purchase with limited funds?

Input:

The input data first contains a positive integer c, which indicates that there is a C set of test cases, the first line of each set of test cases is two integers n and m (1<=n<=100, 1<=m<=100), respectively, the amount of money and the type of rice, then the M row of data, each row contains 3 number p, H and C (1<=p<=20,1<=h<=200,1<=c<=20), respectively, indicating the price per bag, the weight of each bag and the number of bags corresponding to the type of rice.

Output:

For each set of test data, output the maximum weight that can be purchased for rice, and you can assume that you are spending more than all of the rice, and you can afford to spend it. The output for each instance takes up one row.

Sample input:
18 22 100 44 100 2
Sample output:
400

Always wanted to contact the multi-knapsack problem, and today finally met.
This is a multi-knapsack problem, simply put, is that each weight of the item is not only one, is not an infinite, but a limited number of
The first idea is to convert to a common knapsack problem to solve, that is, each category of items as a single item
The code is as follows
1#include <cstdio>2#include <iostream>3#include <cstring>4 using namespacestd;5 intn,m;6 intp[102];7 intw[102];8 intc[102];9 intdp[102];Ten  One intMainintargcChar Const*argv[]) A { -     intC; -     //freopen ("Input.txt", "R", stdin); the      while(SCANF ("%d", &c)! =EOF) { -          while(c--) { -scanf"%d%d",&n,&m); -              for(inti =0; I < m; i++) { +scanf" %d%d%d",&p[i],&w[i],&c[i]); -             } +Memset (DP,0,sizeof(DP)); A              for(inti =0; I < m; i++) { at                  for(intj =1; J <= C[i]; J + +) { -                      for(intk = n; K >= P[i]; k--) { -Dp[k] = max (dp[k],dp[k-p[i]]+w[i]); -                     } -                 } -                  in             } -printf"%d\n", Dp[n]); to         } +     } -     return 0; the}

Of course, there are some ingenious solutions to this problem.

For example, if an item has 6 pieces, if we divide it into 1 + 2 + 3 pieces, you will find that you can make up any 1 to 6 number

Such a splitting method is to divide a number into 2 of the power of the sum, if not the whole, then the last number is poor

For example, 10 can be split into 1 +2 + 4 + 3

So we can use these numbers to make things that are worth something.

The code is as follows:

1#include <cstdio>2#include <iostream>3#include <cstring>4 using namespacestd;5 intn,m;6 intp[2002];7 intw[2002];8 intc[2002];9 intdp[2002];Ten  One intMainintargcChar Const*argv[]) A { -     intC; -     //freopen ("Input.txt", "R", stdin); the      while(SCANF ("%d", &c)! =EOF) { -          while(c--) { -scanf"%d%d",&n,&m); -             intCNT =0; +              for(inti =0; I < m; i++) { -                 intpt,wt,ct; +scanf" %d%d%d",&pt,&wt,&CT); A                 intc =1; at                  while(Ct-c >0) { -P[CNT] = c *pt; -W[CNT] = c *wt; -CT = ct-C; -cnt++; -C *=2; in                 } -P[CNT] = ct*pt; toW[CNT] = ct*wt; +cnt++; -             } theMemset (DP,0,sizeof(DP)); *              for(inti =0; I < CNT; i++) { $                  for(intk = n; K >= P[i]; k--) {Panax NotoginsengDp[k] = max (dp[k],dp[k-p[i]]+w[i]); -                 } the                  +             } Aprintf"%d\n", Dp[n]); the         } +     } -     return 0; $}

Nine degrees OJ topic 1455: Cherish now, Thanksgiving life

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.