Hdu1712: acboy needs your help (group backpack template)

Source: Internet
Author: User

Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1712

Problem DescriptionACboy has N courses this term, and he plans to spend at most M days on study.Of course,the profit he will gain from different course depending on the days he spend on it.How to arrange the M days for the N courses to maximize the profit? InputThe input consists of multiple data sets. A data set starts with a line containing two positive integers N and M, N is the number of courses, M is the days ACboy has.Next follow a matrix A[i][j], (1<=i<=N<=100,1<=j<=M<=100).A[i][j] indicates if ACboy spend j days on ith course he will get profit of value A[i][j].N = 0 and M = 0 ends the input. OutputFor each data set, your program should output a line which contains the number of the max profit ACboy will gain. Sample Input2 21 21 32 22 12 12 33 2 13 2 10 0 Sample Output346

Group backpack

Problem

There are n items and a backpack with a capacity of v. The cost of the I-th item is C [I], and the value is W [I]. These items are divided into several groups. items in each group conflict with each other and you can select at most one item. Solving which items are loaded into a backpack can make the total cost of these items not exceed the capacity of the backpack, and the total value is the largest.

Algorithm

This problem becomes that there are several strategies for each group of items: whether to select one item in the group or not. That is to say, if f [k] [v] is set, it indicates the maximum weight value that can be obtained for the first K items in the Group. The options are:

F [k] [v] = max {f [k-1] [v], f [k-1] [V-C [I] + W [I] | item I belongs to group k}

 

The pseudocode for using a one-dimensional array is as follows:

For all group K
    for v=V..0
For all I belong to group K
            f[v]=max{f[v],f[v-c[i]]+w[i]}

Pay attention to the order of the three-tier loop here, even in the first beta version of this article, I wrote an error myself. The loop "for V = V .. 0" must be outside of "for all I belong to group K. In this way, only one item in each group can be added to the backpack.

 

In addition, it is obvious that you can apply p02 (full backpack problem) to items in each group to "a simple and effective optimization ".

Summary

The grouping of backpacks refers to several items mutually exclusive to each other as a group, which establishes a good model. Many of the bag problems can be transformed into a group's backpack problems (for example, p07 (a dependent backpack problem). The group's backpack problems can further define the concept of "Generalized items, it is very helpful for solving problems.


Algorithm analysis:

This is my first contact with a group backpack.

This is a typical grouping backpack, with each group containing at most one backpack.

After reading about the backpack 9, you just need to put it on the cover he wrote above. Remember every meaning in the triple loop ~!!!

The Code is as follows:
# Include <iostream> using namespace STD; int A [101] [101], F [101]; int main () {int n, m, I, J, K; while (CIN> N> M & (n! = 0 | M! = 0) {memset (F, 0, sizeof (f); for (I = 1; I <= N; I ++) for (j = 1; j <= m; j ++) CIN> A [I] [J]; for (I = 1; I <= N; I ++) // The first cycle: number of groups for (j = m; j> = 0; j --) // The second cycle: capacity volume for (k = 0; k <= J; k ++) // re-cycle 3: K f [J] = max (F [J], f [J-K] + A [I] [k]); cout <F [m] <Endl ;}return 0 ;}

 

Hdu1712: acboy needs your help (group backpack template)

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.