Acm hdu 1074 doing homework (bit operation, search, State compression DP)

Source: Internet
Author: User
Doing homework

Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 1950 accepted submission (s): 685

Problem descriptionignatius has just come back school from the 30th ACM/ICPC. now he has a lot of homework to do. every teacher gives him a deadline of handing in the homework. if Ignatius hands in the homework after the deadline, the teacher will reduce his score of the final test, 1 day for 1 point. and as you know, doing homework always takes a long time. so Ignatius wants you to help him to arrange the order of doing homework to minimize the specified CED score.

Inputthe input contains several test cases. The first line of the input is a single integer T which is the number of test cases. t test cases follow.
Each test case start with a positive integer N (1 <= n <= 15) which indicate the number of homework. then n lines follow. each line contains a string s (the subject's name, each string will at most has 100 characters) and two integers D (the deadline of the subject ), C (How many days will it take Ignatius to finish this subject's homework ).

Note: All the subject names are given in the alphabet increasing order. So you may process the problem much easier.

Outputfor each test case, You shoshould output the smallest total CED score, then give out the order of the subjects, one subject in a line. if there are more than one orders, you should output the alphabet smallest one.

Sample input2
3
Computer 3 3
English 20 1
Math 3 2
3
Computer 3 3
English 6 3
Math 6 3

Sample output2
Computer
Math
English
3
Computer
English
Math

Hint

In the second test case, both computer-> English-> math and computer-> math-> English leads to reduce 3 points, but
Word "English" appears earlier than the word "math", so we choose the first order. That is so-called alphabet order.

 

Authorignatius. L Algorithm Core: Status compression DP
Note:
There are n course assignments. The deadline for each assignment is d, and the time required is C. If the assignment cannot be completed on time, 1 point is deducted for each expiration day.
These n assignments are input in the Lexicographic Order of the course.
How many points should be deducted for completing the n jobs and the sequence of least points should be output.
PS: there are a variety of solutions to achieve the minimum deduction points. Please output the group of solutions with the smallest Lexicographic Order

Analysis:
N <= 15, according to the meaning of the question, you only need to arrange the n jobs in full and select the least points.
Use a binary number to store the completion status of the N jobs, which is 1st .... N jobs
It corresponds to the 0, 1... of the binary number, where N-1 is the meaning of the question. Therefore, the maximum number is 2 ^ n.
2 ^ n-1 indicates that n jobs are completed, and 0 indicates that no jobs are completed...

Use DP [I] to record the information when the job status is I (time required, previous status, minimum loss score ).
The recursive conditions are as follows:
1. The condition that status A can perform job I is that job I in job A has not been completed, that is, a & I = 0.
2. If two statuses DP [a] and DP [B] can reach DP [I], select the path that will make the I deduction smaller. If the score is the same, transfer to 3.
3. If the scores of the two statuses are the same, select the smaller Lexicographic Order. Because the job is input in Lexicographic Order, that is, DP [I]. Pre = min (A, B );

Initialization: DP [0]. Cost = 0; DP [0]. Pre =-1; DP [0]. Ced CED = 0;

The last DP [2 ^ n-1]. Ced CED is the minimum deduction, and the course schedule can be recursively output.

 /*  
Hdu1074
*/
# Include < Stdio. h >
# Include < String . H >
Const Int Maxn = 1 < 16 ;
Struct Node
{
Int Cost; // Time required
Int Pre; // Previous status
Int Reduced; // Minimum Loss score
} DP [maxn];
Bool Visited [maxn];
Struct Course
{
Int Deadtime; // Deadline
Int Cost; // Date required
Char Name [ 201 ];
} Course [ 16 ];
Void Output ( Int Status)
{
Int Curjob = DP [Status]. Pre ^ Status;
Int Curid = 0 ;
Curjob >>= 1 ;
While (Curjob)
{
Curid ++ ;
Curjob >>= 1 ;
}
If (DP [Status]. Pre ! = 0 )
{
Output (DP [Status]. PRE );
}
Printf ( " % S \ n " , Course [curid]. Name );
}
Int Main ()
{
Int T, n, I, j;
Scanf ( " % D " , & T );
While (T -- )
{
Scanf ( " % D " , & N );
Int Upper = 1 < N;
Int Dayupper = 0 ;
For (I = 0 ; I < N; I ++ )
{
Scanf ( " % S % d " , & Course [I]. Name, & Course [I]. deadtime, & Course [I]. cost );
Dayupper + = Course [I]. cost;
}
Memset (visited, False , Sizeof (Visited ));
DP [ 0 ]. Cost = 0 ;
DP [ 0 ]. Pre =- 1 ;
DP [ 0 ]. Ced CED = 0 ; // DP [0] indicates the status in which no job is executed.
Visited [ 0 ] = True ;
Int Work;
Int Tupper = Upper - 1 ; // Tupper indicates that the binary number is N 1, indicating that all jobs have been completed.
For (J = 0 ; J < Tupper; j ++ ) // Traverse all statuses
{
For (Work = 0 ; Work < N; Work ++ )
{
Int Cur = 1 < Work;
If (Cur & J) = 0 ) // This job has not been done
{
Int Curtemp = Cur | J;
Int Day = DP [J]. Cost + Course [work]. cost;
DP [curtemp]. Cost = Day;
Int Reduce = Day - Course [work]. deadtime;
If (Reduce < 0 ) Reduce = 0 ;
Reduce + = DP [J]. Ced CED;
If (Visited [curtemp]) // Existing access information in this status
{
If (Reduce < DP [curtemp]. Ced CED)
{
DP [curtemp]. Ced CED = Reduce;
DP [curtemp]. Pre = J;
}
// Else if (reduce = DP [curtemp]. Ced CED)
// The deduction points are the same, and the Lexicographic Order is small. Because J is searched from small to small, it is in Lexicographic Order by default and does not need to be processed.
// {
// If (DP [curtemp]. PRE> J)
// DP [curtemp]. Pre = J;
// }
}
Else // No access
{
Visited [curtemp] = True ;
DP [curtemp]. Ced CED = Reduce;
DP [curtemp]. Pre = J;
}
}
}
}
Printf ( " % D \ n " , DP [Tupper]. Ced CED );
Output (Tupper ); // Recursive output
}
}

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.