Hdu oj 1712 ACboy needs your help [grouping backpack entry question]

Source: Internet
Author: User

Question: n represents the total number of classes, and m represents the number of days.
The following is a matrix of n * m: The j-th value of line I represents the j-day benefit of section I .. If it takes no more than m to attend classes, how can we get the greatest benefit!
Contact 01 backpack. The difference is that each class can only last once at most !! That is, the items in each row in the matrix are either taken or not taken !! This is a grouping backpack entry !!
Group backpack: a backpack with N items and 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. Solve which items can be loaded into a backpack
So that the total cost of these items does not exceed the capacity of the backpack, and the total value is the largest. This problem of algorithms 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, f [k] [v] indicates the first k items in the group.
The maximum value of the cost v can be obtained:
F [k−1] [v]
F [k] [v] = Max {
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 :()
1 for All group k
2 do for v convert V to 0
3 do for all I belong to group k
4 do f [v] = max {f [v], f [v −c [I] + w [I]}
Note: The order of the layer-3 loop here, "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 ..
AC code:
[Cpp]
# Include <stdio. h>
# Include <string. h>
Struct hello
{
Int x;
Int y;
} OK [10005] [10005];
Int yi [1000];
Int main ()
{
Int a, B, c, n, m;
While (scanf ("% d", & n, & m) & (n + m ))
{
Memset (yi, 0, sizeof (yi ));
For (a = 1; a <= n; a ++)
{Www.2cto.com
For (B = 1; B <= m; B ++)
{
OK [a] [B]. x = B;
Scanf ("% d", & OK [a] [B]. y );
}
}
For (a = 1; a <= n; a ++)
{
For (c = m; c> = 0; c --)
{
For (B = 1; B <= m; B ++)
If (c> = OK [a] [B]. x & yi [c] <yi [c-OK [a] [B]. x] + OK [a] [B]. y)
Yi [c] = yi [c-OK [a] [B]. x] + OK [a] [B]. y;
}
}
Printf ("% d \ n", yi [m]);
}
Return 0;
}

 

Author: PIAOYI0208

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.