Uva10130-SuperSale (01 backpack)

Source: Internet
Author: User

Uva10130-SuperSale (01 backpack)

Question: uva10130-SuperSale (01 backpack)


Question: Supermarket sales. There are n items, each of which has the corresponding value and weight. One family wants to go to the supermarket to buy things. Each person can only buy one item for each type of sale. They can take many different items but can take them. Give everyone the maximum weight they can take, and ask such a family to obtain the maximum value they can get from purchasing.


Solution: 01 backpack. Dp [j] = Max (dp [j], dp [j-W] + P); W is the weight of the product, and P is the value of the product.


Code:

#include 
 
  #include 
  
   const int N = 10005;const int maxn = 35;int dp[maxn];int object[N][2];int Max (const int a, const int b) { return a > b ? a: b; }int main () {int t;int n, g;int w;int ans;scanf ("%d", &t);while (t--) {scanf ("%d", &n);for (int i = 0; i < n; i++)scanf ("%d%d", &object[i][0], &object[i][1]);scanf ("%d", &g);memset (dp, 0, sizeof (dp));for (int i = 0; i < n; i++) for (int j = maxn - 5; j >= object[i][1]; j--)dp[j] = Max(dp[j], dp[j - object[i][1]] + object[i][0]);ans = 0;for (int i = 0; i < g; i++) {scanf ("%d", &w);ans += dp[w];}printf ("%d\n", ans);}return 0;}
  
 




Related Article

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.