Ultraviolet A 10130 SuperSale (dp + 01 backpack)

Source: Internet
Author: User

Here is a SuperSale in a SuperHiperMarket. every person can take only one object of each kind, I. e. one TV, one carrot, but for extra low price. we are going with a whole family to that SuperHiperMarket. every person can take as example objects, as he/she can carry out from the SuperSale. we have given list of objects with prices and their weight. we also know, what is the maximum weight that every pe Rson can stand. What is the maximal value of objects we can buy at SuperSale? Input SpecificationThe input consists of T test cases. the number of them (1 <= T <= 1000) is given on the first line of the input file. each test case begins with a line containing a single integer number N that indicates the number of objects (1 <= N <= 1000 ). then follows Nlines, each containing two integers: P and W. the first integer (1 <= P <= 100) corresponds to the price of object. the second intege R (1 <= W <= 30) corresponds to the weight of object. next line contains one integer (1 <= G <= 100) it's the number of people in our group. next G lines contains maximal weight (1 <= MW <= 30) that can stand this I-th person from our family (1 <= I <= G ). output SpecificationFor every test case your program has to determine one integer. print out the maximal value of goods which we can buy with that family. sample Input2372 1744 2331 24126664 2685 2252 499 1839 1354 Output for the Sample Input72514 question: I am buying g products. Everyone has a certain weight of mw and n products, each item has a value of p and weight of w. All users are required to purchase each item only once, and the total value of the last purchase is the largest. Idea: 01 backpack problem. The state transition equation is dp [j] = max (dp [j-w [I] + p [I], dp [j]). Code:

#include <stdio.h>#include <string.h>int t, n, p[1005], w[1005], g, mw, i, j, dp[30005], sum;int max(int a, int b) {return a > b ? a : b;}int main() {scanf("%d", &t);while (t --) {sum = 0;int s = 0;scanf("%d", &n);for (i = 0; i < n; i ++) {scanf("%d%d", &p[i], &w[i]);s += w[i];}memset(dp, 0, sizeof(dp));for (i = 0; i < n; i ++)for (j = s; j >= w[i]; j --) {if (dp[j - w[i]] || j - w[i] == 0)dp[j] = max(dp[j - w[i]] + p[i], dp[j]);}scanf("%d", &g);while (g --) {int Max = 0;scanf("%d", &mw);for (i = 0; i <= mw; i ++)Max = max(Max, dp[i]);sum += Max;}printf("%d\n", sum);}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.