HDU 1074:doing Homework (pressure DP)

Source: Internet
Author: User

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): 7704 Accepted Submission (s): 3484

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 each test case, you should output the smallest total reduced score and then give out the order of the subjects, one subje CT in a line. If there is more than one orders, you should output the alphabet smallest one. Sample Input2Computer 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. Test instructions: There are n courses of work needs to be completed, each job has a deadline and a time spent, if the completion of the course of work more than deadline, each more than a day will deduct a point, how to arrange the completion of the order of the job to make the completion of all the job deduction of the minimum score, The minimum score is how much, and output the order of doing the homework. Idea: first-line pressure DP. Start innocently want to do with greed, and later found not to do, think for a long time to see the puzzle, the original is afraid of a long-form pressure DP, because there are a lot of bit arithmetic do not understand, and then their own evil to fill a bit of knowledge, counted vaguely understand it. Because n is small, you can enumerate all cases. 5 can be represented as a binary 1001, from the right to the left, the state represents the completion of the first and fourth doors, that is, 1 or zero to indicate that the point has not passed, and then enumerate 1<<i (i<n) is equivalent to enumerate each section of the job, now&i = = 0 for the i+ The 1-door job was not completed and now is a collection of jobs that have been completed. In this way, DP solution is possible.
1#include <cstdio>2#include <cstring>3#include <algorithm>4#include <string>5#include <map>6#include <stack>7#include <iostream>8 using namespacestd;9 #defineN 15Ten #defineINF 100000000 One structnode A { -     intDead,cost; -     Chars[ the]; the}course[n+1]; - structP - { -     intPre,score,now,time; + //pre record a collection of disciplines that have been completed prior to completion of a subject's assignment - //now records the currently updated disciplines, pre and current as path outputs + //score is the total number of credits reduced, time is current A}dp[1<<N]; at intvis[1<<N]; -  - intMain () - { -     intT; -Cin>>T; in      while(t--){ -         intN; toCin>>N; +Memset (DP,0,sizeof(DP)); -memset (Vis,0,sizeof(Vis)); the          for(intI=0; i<n;i++){ *Cin>>course[i].s>>course[i].dead>>Course[i].cost; $         }Panax Notoginseng         intEn= (1<<n)-1; -          for(intI=0; i<en;i++){ the              for(intj=0; j<n;j++){ +                 inttemp=1<<J; A                 if((i&temp) = =0){//If the current course of work is not completed the                     intCur=i|temp;//when the work of the section is completed +Dp[cur].time=dp[i].time+course[j].cost;//current time of Use -                     intb=dp[i].time+course[j].cost-Course[j].dead; $                     if(b<0) b=0; $                     if(Vis[cur]) {//Update If the job has been completed -                         if(dp[cur].score>dp[i].score+b) { -Dp[cur].score=dp[i].score+b; theDp[cur].pre=i;//Have not finished the subject that has been completed before the subject -Dp[cur].now=j;//the current subjectWuyi                         } the                     } -                     Else{//If you do not complete the direct update Wuvis[cur]=1; -Dp[cur].score=dp[i].score+b; AboutDp[cur].pre=i; $dp[cur].now=J; -                     } -                 } -             } A         } +cout<<dp[en].score<<Endl; theStack <int>S; -         //use the stack output to complete the path of the job, or you can use recursion $          while(en>0){ the S.push (dp[en].now); theen=Dp[en].pre; the         } the          while(!S.empty ()) { -Cout<<course[s.top ()].s<<Endl; in S.pop (); the         } the     } About     return 0; the}

2016-06-26

HDU 1074:doing Homework (pressure 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.