Nyoj 49 Happy Xiaoming

Source: Internet
Author: User
Tags ming

Happy Xiaoming time limit:MS | Memory limit:65535 KB Difficulty:4
Describe
Xiao Ming is very happy today, the home purchase of the new house on the key, the new house has a very spacious room of his own dedicated. To his delight, the mother said to him yesterday: "Your room needs to buy what items, how to decorate, you decide, as long as not more than n yuan money on the line." Early this morning, Xiao Ming began to budget, but he wanted to buy too many things, will certainly exceed the mother limit of N yuan. As a result, he set an important degree of each article, divided into 5, such as: The whole number of the 5th, and so the most important. He also found the price of each item (all integers) from the Internet. He hoped that the sum of the product of the price and the importance of each item would be the largest if not more than n yuan (which could equal N). Set the price of the article J items for V[j], the importance of w[j], a total of selected K items, numbered sequentially j1...jk, then the sum of the total is: v[j1]*w[j1]+. +V[JK]*W[JK] Please help jinming to design a shopping list that satisfies the requirements.
Input
The first line enters an integer N (0<n<=101) that indicates the number of test data groups
The 1th line of the test data input for each group is two positive integers separated by a space:
Nm
(where n (<30000) represents the total amount of money, M (<25) is the number of items you wish to purchase. )
From line 2nd to line m+1, line J gives the number J-1
The basic data of the items, each line has 2 non-negative integers
V P
(where v indicates the price of the item (v≤10000), p indicates the importance of the item (
)
Output
each set of test data outputs has only one positive integer, which is the sum of the product's price and importance for items not exceeding the total amount of money.
Maximum value (<100000000)
Sample input
11000 5800 2400 5300 5400 3200 2
Sample output
3900

This question belongs to the 01 knapsack problem, the core is 01 knapsack of the state transition equation for this question to set P[i] for the object of the amount of money, z[i] for the importance of the object, and another array dp[i],n for the total money held, m for the total number of items to buy the problem of the conversion equation is Dp[n]=max (dp[i ],dp[n-p[i]]+z[i]*p[i]) explain: When comparing each set of data, the backpack capacity is changed from N, to the end of the price of this set of data, the first set of data, the price is 800, the importance of 2, then the backpack capacity is reduced, to 800 stops, the array dp[ 800] to dp[1000] has been assigned the initial value, and then compare the second set of data if the result of the second set of data is larger than the first set of data with a large value covering the front of the small value, and so of course, when the amount of money in the back and not more than N, then take and seek to the maximum value

Enclosed AC Code

#include <stdio.h> #include <string.h>int main () {int dp[50000];     Note that this array of prices cannot be opened too small int n,n,m,j,i;int w[30],p[30]; scanf ("%d", &n), while (n--) {memset (dp,0,sizeof (DP)); scanf ("%d%d", &n,&m); for (i=0;i<m;i++) scanf ("%d %d ", &w[i],&p[i]); for (i=0;i<m;i++) {for    (j=n;j>=w[i];j--)    {    if (dp[j]<dp[j-w[i]]+p[i ]*w[i])  //The Core state transition equation of the problem         dp[j]=dp[j-w[i]]+p[i]*w[i];}    } printf ("%d\n", Dp[n]);} return 0;}

  

Nyoj 49 Happy Xiaoming

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.