HDU 1074 doing homework (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): 3595 accepted submission (s): 1424

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 previous questions. Review DP and compress DP, write more natural understanding, more in-depth
/*  HDU 10741 <= n <= 15 adopts the State compression DP and requires the output sequence. Use the pre array. Since the lexicographically arranged at the beginning, the smallest lexicographically ordered solution can be output from small to large DP  */  # Include <Stdio. h> # Include <Algorithm> # Include <Iostream> # Include < String . H> Using   Namespace  STD;  Const   Int Maxn =16  ;  Const   Int INF = 0x3f3f3f  ;  Struct  Node {  Char Name [ 110  ];  Int  D, C;} node [maxn];  Int DP [ 1 < Maxn]; Int Pre [ 1 < Maxn];  Int  N;  Void Output ( Int  Status ){  If (Status = 0 ) Return  ;  Int T = 0  ; For ( Int I = 0 ; I <n; I ++ )  If (Status &( 1 <I ))! = 0 & (Pre [Status] & ( 1 <I) = 0  ) {T = I;  Break  ;} Output (pre [Status]); printf ( "  % S \ n  "  , Node [T]. Name );}  Int  Main (){  Int  T; scanf (  "  % D  " ,& T );  While (T -- ) {Scanf (  "  % D " ,& N );  For ( Int I = 0 ; I <n; I ++ ) Scanf (  "  % S % d  " , & Node [I]. Name, & node [I]. D ,& Node [I]. C );  For ( Int I = 0 ; I <( 1 <N); I ++ ) DP [I] = INF; DP [  0 ] = 0  ;  For ( Int I = 0 ; I <( 1 <N); I ++ ){  For ( Int J = 0 ; J <n; j ++){  If (I &( 1 <J )) Continue  ;  Int S = 0  ;  For ( Int K = 0 ; K <n; k ++ )  If (I &( 1 <K) S + = Node [K]. C; s + = Node [J]. C;  If (S> node [J]. d) S = s- Node [J]. D;  Else S = 0  ;  If (DP [I | ( 1 <J)]> DP [I] + S) {DP [I | ( 1 <J)] = DP [I] +S; Pre [I | ( 1 <J)] = I ;}} printf (  "  % D \ n  " , DP [( 1 <N )- 1  ]); Output ((  1 <N )- 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.