RQNOJ PID2 happy Jinming

Title Description

Jinming I am very happy, the family purchased a new house on the key essentials, there is a new room dedicated himself very spacious room. He became even more pleased that my mother yesterday and said to him: "You need room which items to buy, how layout, you have the final say, as long as no more than N dollars on the line." This morning Jinming started doing the budget, but he wanted to buy so many things will certainly exceed the limit N mother yuan. Thus, each item he specifies a degree of importance, and the like is divided into 5: 1 to 5 show an integer, fifth, etc. most important. He also found the price of each item (are integers dollars) from the Internet. He wants without exceeding N element (element may be equal to N) of the sum of the products and the degree of importance of the price of each item maximum. The first set price for items j V [j], a degree of importance W [j], a total of k selected items, the order of numbered j1 ... jk, then the required sum is: v [j1] * w [j1] + .. + v [jk] * w [jk] Please help Jinming designed to meet the requirements of a shopping list.

Input Format

1 input from the first row, separated by a space as two positive integers:

N m

(Where N (<30000) represents the total amount of money, m (<25) to a desired number of items purchased.)

From row 2 to row m + 1, j-th row gives the number j-1

Basic data items, each row has two non-negative integers

v p

(Where v represents the price of the item (v≤10000), p indicates the degree of importance of the article (1 to 5))

Output Format

Output only a positive integer, and the importance of the product for the price does not exceed the total amount of money the sum of the items

The maximum value (<100 000 000)

Sample input

1000 5
800 2
400 5
300 5
400 3
200 2

Sample Output

3900

Problem-solving ideas

  This title is a look at knapsack problem, but I just started resolutely with greedy or do so only 30 ';

  This problem is a modification of the backpack 01, the state transition equation : F [J] = max {F [J], F [JW [I]] + W [I] * V [I]};

code show as below

 1 #include<iostream>
 2 #include<algorithm>
 3 using namespace std;
 4 struct node{
 5     int v, w, s; 
 6 }t[40010];
 7 int f[40010];
 8 int main(){
 9     int n, m;
10     cin >> n >> m;
11     for(int i = 1; i <= m; i++){
12         int x, y;
13         cin >> x >> y;
14         t[i].v = x;    t[i].w = y;    t[i].s = x * y;
15     }
16     for(int i = 1; i <= m; i++){
17         for(int j = n; j >= t[i].v; j--){
18             f[j] = max(f[j], f[j - t[i].v] + t[i].s);
19         }
20     }
21     cout << f[n] << endl;
22     return 0;
23 }
Happy Jinming

 


 

Guess you like

Origin www.cnblogs.com/zoom1109/p/11041466.html