Hdu_1074_doing Homework_ State Compression DP

Source: Internet
Author: User

Links:http://acm.hdu.edu.cn/showproblem.php?pid=1074

Doing Homework

Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 7543 Accepted Submission (s): 3375


Problem Descriptionignatius have just come back school from the 30th ACM/ICPC. Now he had 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 would reduce his score of the final Test, 1 day for 1 poi Nt. And as you know, doing homework always takes a long time. So Ignatius wants-to-help him to arrange the order of doing homework to minimize the reduced score.


Inputthe input contains several test cases. The first line of the input was a single integer T which is the number of test cases. T test Cases follow.
Each test case is 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 would at the most have characters) and a integers D (the dead Line of the subject), C (what many days would it take Ignatius to finish this subject ' s homework).

Note:all the subject names is given in the alphabet increasing order. So, may process the problem much easier.


Outputfor should output the smallest total reduced score and then give out the order of the subjects, one Subject in a line. If there is more than one orders, you should output the alphabet smallest one.


Sample input23computer 3 3English 1Math 3 23Computer 3 3English 6 3Math 6 3


Sample Output2computermathenglish3computerenglishmath HintIn the second Test case, both Computer->english->math and computer->math->english leads to reduce 3 points, BU t the word "中文版" appears earlier than the word "Math", so we choose the first order. That is so-called alphabet order. At first I wanted to do it with greed and failed to make it. Read the puzzle is the state of the compression DP, read the puzzle to write their own once. The idea is well understood, with a 15-bit binary number to represent all States. There is a path in the middle of the operation, can learn from later.
#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<stack>using namespacestd;Const intinf=1<< -;structnode{Charname[ the]; intDead,cost;} cla[ -];structdp{intPre,time,spent,now;} dp[1<< the];intMain () {intT,i,j,s,n,end; //scanf ("%d", &t);Cin>>T;  while(t--) {memset (DP,0,sizeof(DP)); //scanf ("%d", &n);Cin>>N;  for(intI=0; i<n;i++)            //scanf ("%s%d%d", cla[i].name,&cla[i].dead,&cla[i].cost);Cin>>cla[i].name>>cla[i].dead>>Cla[i].cost; inten=1<<N;  for(ints=1; s<en;s++) {dp[s].spent=INF;  for(inti=n-1; i>=0; i--)            {                inttem=1<<i; if(s&tem) {                    intpast=s-tem; intst=dp[past].time+cla[i].cost-Cla[i].dead; if(st<0) St=0; if(dp[s].spent>dp[past].spent+St) {dp[s].spent=dp[past].spent+St; Dp[s].now=i; Dp[s].pre=past; Dp[s].time=dp[past].time+Cla[i].cost; } }}} Stack<int>S; intTEM = en-1; cout<< dp[tem].spent <<Endl;  while(TEM) {s.push (Dp[tem].now); TEM=Dp[tem].pre; }         while(!S.empty ()) {cout<< cla[s.top ()].name <<Endl;        S.pop (); }    }    return 0;}
View Code

Hdu_1074_doing Homework_ State Compression DP

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.