Hdu 3535 AreYouBusy (DP _ backpack)

Source: Internet
Author: User

XiaoA wants to spend as much time as possible to do ACM, but the boss asks him to finish n heap jobs within t time, and each job consumes cost [I] [j], happiness val [I] [j]. Each job has num [I] jobs, and each heap of jobs has a nature. 0 indicates that at least one job should be done, 1 indicates that one job can be done at most, and 2 indicates that the job can be done or not done at will. Finally, I asked my boss how to achieve the highest happiness when meeting the boss's requirements.-1.

Solution: Is this a hybrid backpack? None of Nima's mixed state transfer equations can be written.
Each heap can be considered as a group, and each group has its own state transition equation;
When a group is 0, it is a group backpack deformation. Each time the status is transferred from the previous group to the current group, it can be transferred from one place. This group is legal.
When a group is 1, it is a group backpack, but here we use a two-dimensional array. After a group is computed, We need to copy the results of the previous group.
When a group is of the nature of 2, you can use this group as the 01 backpack. Remember to copy the results of the previous Group because this group is optional.
There is a trick in this question, that is, the capacity starts from 0, which is different from the conventional capacity starting from 1. Pay attention to the lower bound in the for loop.

Test data:
1 0
2 0
0 1
0 2


1 0
2 2
0 1
0 2


1 1000
2 0
1 1
2 2

Code:
[Cpp]
# Include <stdio. h>
# Include <string. h>
# Deprecision MAX 102
# Define max (a, B) (a)> (B )? (A): (B)
 
 
Int ans, dp [MAX] [MAX];
Int n, m, num [MAX], flag [MAX];
Int cost [MAX] [MAX], val [MAX] [MAX];
 
 
Int Solve_1A (){
 
Int I, j, k, tpval;
 
 
Dp [0] [0] = 0;
For (I = 1; I <= n; ++ I ){

If (flag [I] = 2 ){
// 01 backpack www.2cto.com
For (k = 1; k <= num [I]; ++ k)
For (j = m; j> = cost [I] [k]; -- j ){

Tpval = dp [I] [j-cost [I] [k];
If (tpval! =-1) dp [I] [j] = max (dp [I] [j], tpval + val [I] [k]);
Tpval = dp [I-1] [j-cost [I] [k];
If (tpval! =-1) dp [I] [j] = max (dp [I] [j], tpval + val [I] [k]);

}
For (j = 0; j <= m; ++ j)
Dp [I] [j] = max (dp [I] [j], dp [I-1] [j]);
}
Else if (flag [I] = 1 ){
// Group a backpack. select one of the most to ensure that dp [I] [j] is transferred only from the last status.
For (j = m; j> = 0; -- j)
For (k = 1; k <= num [I]; ++ k)
If (j> = cost [I] [k]) {

Tpval = dp [I-1] [j-cost [I] [k];
If (tpval! =-1) dp [I] [j] = max (dp [I] [j], tpval + val [I] [k]);
}
For (j = 0; j <= m; ++ j)
Dp [I] [j] = max (dp [I] [j], dp [I-1] [j]);
}
Else {
// Select at least one group backpack
For (k = 1; k <= num [I]; ++ k)
For (j = m; j> = cost [I] [k]; -- j ){

Tpval = dp [I] [j-cost [I] [k];
If (tpval! =-1)
Dp [I] [j] = max (dp [I] [j], tpval + val [I] [k]);
Tpval = dp [I-1] [j-cost [I] [k];
If (tpval! =-1)
Dp [I] [j] = max (dp [I] [j], tpval + val [I] [k]);

}
}
 
// This group is invalid.-1 can be returned directly.
For (j = 0; j <= m; ++ j)
If (dp [I] [j]! =-1) break;
If (j = m + 1) return-1;
}
 

For (ans =-1, I = 0; I <= m; ++ I)
Ans = max (ans, dp [n] [I]);
Return ans;
}
 
 
Int main ()
{
Int I, j, k, t, tpval;
 
 
While (scanf ("% d", & n, & m )! = EOF ){
 
Memset (dp,-1, sizeof (dp ));
Memset (num, 0, sizeof (num ));
For (I = 1; I <= n; ++ I ){
 
Scanf ("% d", & num [I], & flag [I]);
For (j = 1; j <= num [I]; ++ j)
Scanf ("% d", & cost [I] [j], & val [I] [j]);
}
 
 
Int ans = Solve_1A ();
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.