Time limit: +Ms | Memory Limit:65535KB 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
#include <iostream> #include <cstring> #include <cmath>using namespace Std;int ans[30000],v[25],p[25 ];int Main () {int T,n,m,i,j,k;cin>>t;while (t--) {cin>>n>>m;for (i=0;i<m;++i) cin>>v[i] >>p[i];memset (ans,0,sizeof (ans)); for (i=0;i<m;++i) for (J=N;J>=V[I];--J) if ((ans[j-v[i]]>0) | | (0==j-v[i])) Ans[j]=max (Ans[j],ans[j-v[i]]+v[i]*p[i]); K=0;for (i=0;i<=n;i++) K=max (K,ans[i]); Cout<<k<<endl;} return 0;}
Nyoj 49 Happy Xiaoming (01 knapsack problem)