Hdu 1712 ACboy needs your help (DP _ backpack)

Source: Internet
Author: User

Give n courses, review the n courses in m days, and then give A matrix A of n * m, A [I] [j] indicates the benefits of reviewing the I course on day j. Q: The maximum benefit of reviewing different classes in m days.

Solution: This question is not difficult, but the data is very confusing. At first I thought that one course can be reviewed twice, then, we processed n * m items as n * m items in a 01 backpack, and submitted a gorgeous return Wa. After reading the questions again, You must select a different course. This is the grouping of backpacks, so that you can only choose a review for this course.
State transition equation: dp [k] = min (dp [k], dp [k-j] + pro [I] [j]) (I indicates the I-th course, k indicates the number of days, and pro [I [j indicates the number of j-days for the I-th Department ). complexity (O (n * m ). note that the enumeration sequence of the number of days must be large to small, so as to ensure that the State required for each calculation is the previous one, in order to ensure the efficiency.

Test data:
2 3
4 4 1
3 2 1

1 2
4

Code:
[Cpp]
# Include <stdio. h>
# Include <string. h>
# Deprecision MAX 110
 
 
Int n, m, ans, dp [MAX];
Int pro [MAX] [MAX];
 
 
Int main ()
{
Int I, j, k, t;
 
 
While (scanf ("% d", & n, & m), n + m ){
 
For (I = 1; I <= n; ++ I)
For (j = 1; j <= m; ++ j)
Scanf ("% d", & pro [I] [j]);
 
 
Memset (dp, 0, sizeof (dp ));
For (I = 1; I <= n; ++ I)
For (k = m; k> = 1; -- k)
For (j = k; j> = 1; -- j)
If (dp [k-j] + pro [I] [j]> dp [k])
Dp [k] = dp [k-j] + pro [I] [j];


For (ans = I = 0; I <= m; ++ I)
If (dp [I]> ans) ans = dp [I];
Printf ("% d \ n", ans );
}
}

 


From ZeroClock

Related Article

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.