HDU 1074 doing homework (pressure DP)

Source: Internet
Author: User
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 Input
23Computer 3 3English 20 1Math 3 23Computer 3 3English 6 3Math 6 3
 
Sample output
2ComputerMathEnglish3ComputerEnglishMathHintIn the second test case, both Computer->English->Math and Computer->Math->English leads to reduce 3 points, but the word "English" appears earlier than the word "Math", so we choose the first order. That is so-called alphabet order. 
 
Authorignatius. l the first State to compress DP-like pressure DP. The idea is to use binary (many are used) 01 to indicate a certain State. In this question, a course is not done. Check whether DP [I] contains a job in the recursive relationship, that is, whether a job can be used to reach DP [I]. Thanks: Click to open the link.
# Include <iostream> # include <cstdio> # include <cstring> # include <algorithm> # include <limits. h> using namespace STD; const int maxn = 1 <15; char s [20] [110]; int DP [maxn], t [maxn], pre [maxn]; // DP [I] stores various statuses of jobs. t [I] indicates the elapsed time, And pre [I] indicates the precursor int dead [20] of a certain State. fin [20]; void print (int x) {If (! X) return; print (X-(1 <PRE [x]); printf ("% s \ n", s [pre [x]);} int main () {int TT, N; scanf ("% d", & TT); While (TT --) {scanf ("% d", & N ); memset (T, 0, sizeof (t); memset (PRE, 0, sizeof (pre); For (INT I = 0; I <n; I ++) scanf ("% S % d", & S [I], & dead [I], & Fin [I]); int end = 1 <N; for (INT I = 1; I <end; I ++) {DP [I] = int_max; For (Int J = n-1; j> = 0; j --) // enumerate from the back to {int temp = 1 <j; If (! (I & temp) continue; int cost = T [I-Temp] + FIN [J]-dead [J]; // time consumption if (cost <0) cost = 0; If (DP [I]> DP [I-Temp] + cost) {DP [I] = DP [I-Temp] + cost; T [I] = T [I-Temp] + FIN [J]; Pre [I] = J; // cout <I <"" <j <Endl ;}} printf ("% d \ n", DP [end-1]); // end-1 is the status print (end-1);} return 0 ;}


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.