Nyist oj 311 full backpack (classic question of Dynamic Planning), nyistoj

Source: Internet
Author: User

Nyist oj 311 full backpack (classic question of Dynamic Planning), nyistoj
Full backpack time limit: 3000 MS | memory limit: 65535 KB difficulty: 4

Description

A full backpack defines N items and a backpack with a capacity of V. Each item has an unlimited number of items available. The volume of item I is c and the value is w. Solving which items are loaded into a backpack can make the total size of these items not exceed the total size of the backpack, and the total value is the largest. This question requires that when a backpack is filled with a backpack, find the sum of the maximum value. If it cannot be filled with a backpack, output NO

Input
Row 1: N indicates the number of groups of test data (N <7 ).
Next, the first row of test data in each group contains two integers, M and V. M indicates the number of item types, and V indicates the total size of the backpack. (0 <M <= 2000,0 <V <= 50000)
Each row in the next M row has two integers c, w representing the weight and value of each item respectively (0 <c <100000, 0 <w <)
Output
Corresponding to the output results of each group of test data (if it can be filled with a backpack, the maximum value of the items in the backpack is output when it is filled with a backpack. If it cannot be filled with a backpack, output NO)
Sample Input
21 52 22 52 25 1
Sample output
NO1
Uploaded

ACM _ Zhao minghao

The classic question is dynamically planned. There are also several ideas. The optimal idea is to change the order of the second round of the 01 backpack question to obtain the optimal solution of the complete backpack;

This algorithm uses a one-dimensional array. first look at the pseudocode: (the content in the reference backpack 9 Lecture)

for i=1..N
    for v=0..V
        f[v]=max{f[v],f[v-cost]+weight}

You will find that this pseudo code is different from the pseudo code of the 01 backpack only in the loop order of v. Why is this change feasible? First, let's think about why the v = V .. 0 in P01 should be reversed. This is because we need to ensure that the State f [I] [v] in the I-th loop is recursive from the state f [I-1] [v-c [I. In other words, this is to ensure that each item is selected only once, and to ensure that the policy of "selecting the I-item" is considered, it is based on a sub-result f [I-1] [v-c [I] That has never been selected for item I. Now, the unique feature of a backpack is that each type of item can be an unlimited number of items. Therefore, when considering the policy of "adding a first item, however, you need a sub-result f [I] [v-c [I] that may have been selected for Type I. Therefore, you can use v = 0 .. v. This is why this simple program is established.

It is worth mentioning that the order of the two-layer for loop in the above pseudo code can be reversed. This conclusion may lead to optimization of the algorithm time constant.

This algorithm can also be derived from other ideas. For example, explicitly write the state transition equation for solving f [I] [v-c [I] in the basic idea into the original process, we will find that this equation can be equivalent to this form:

f[i][v]=max{f[i-1][v],f[i][v-c[i]]+w[i]}

This equation is implemented using a one-dimensional array and the above pseudo code is obtained.

The following is the implementation code. The dynamic planning code is very simple. The most important thing is to master the state transition equation:

# Include <cstdio> # include <cstring> # define max (a, B) a> B? A: bconst int maxn = 50001; int dp [maxn]; int main () {int n, m, v, I, j, c, w; scanf ("% d", & n); while (n --) {memset (dp,-10000, sizeof (dp); // pay attention here, in the 01 backpack, the value is initialized to 0. here we need to initialize a relatively large negative number dp [0] = 0; // pay attention here, if this parameter is not set, wa scanf ("% d", & m, & v); for (I = 1; I <= m; I ++) {scanf ("% d", & c, & w); for (j = c; j <= v; j ++) dp [j] = max (dp [j], dp [j-c] + w ); // The state transition equation is also consistent with the 01 backpack.} if (dp [v] <0) printf ("NO \ n"); else printf ("% d \ n ", dp [v]);} return 0 ;}





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.