hdu 1074 Doing Homework
Doing Homework
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4228 Accepted Submission(s): 1705
Problem Description
Ignatius 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 reduced score.
Input
The 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.
Output
For each test case, you should output the smallest total reduced 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
2
3
Computer 3 3
English 20 1
Math 3 2
3
Computer 3 3
English 6 3
Math 6 3
Sample Output
2
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 the
word "English" appears earlier than the word "Math", so we choose the first order. That is so-called alphabet order.
Author
Ignatius.L
1 //15MS 944K 1942 B G++ 2 //第一题状态压缩DP..思路比较清晰~ 3 #include<iostream> 4 #include<stack> 5 using namespace std; 6 struct node{ 7 int pre; 8 int nowtime; //当前时间 9 int cost; //最少花费 10 int key; //当前处理的作业 11 }dp[1<<20]; 12 struct homework{ 13 string name; 14 int d; 15 int c; 16 }hw[20]; 17 int main(void) 18 { 19 int t,n; 20 scanf("%d",&t); 21 while(t--) 22 { 23 scanf("%d",&n); 24 for(int i=0;i<n;i++) 25 cin>>hw[i].name>>hw[i].d>>hw[i].c; 26 dp[0].nowtime=0; 27 dp[0].cost=0; 28 dp[0].pre=-1; 29 int number=1<<n; 30 for(int i=1;i<number;i++){ //总状态数 31 dp[i].cost=0x7ffffff; 32 for(int j=n-1;j>=0;j--){ 33 //printf("i=%d j=%d i&(1<<j)=%d\n",i,j,i&(1<<j)); 34 if(i&(1<<j)){ 35 int id=i-(1<<j); //前一状态(i^(1<<j)) 36 int temp=0; 37 if(hw[j].c+dp[id].nowtime>hw[j].d) 38 temp=dp[id].nowtime+hw[j].c-hw[j].d; 39 else temp=0; 40 //printf("*****temp=%d dp[%d].cost=%d\n",temp,i,dp[i].cost); 41 if(dp[id].cost+temp<dp[i].cost){ 42 dp[i].cost=dp[id].cost+temp; 43 dp[i].nowtime=dp[id].nowtime+hw[j].c; 44 dp[i].pre=id; 45 dp[i].key=j; 46 } 47 //printf("*****dp[%d]-->cost=%d pre=%d nowtime=%d key=%d\n",i,dp[i].cost,dp[i].pre,dp[i].nowtime,dp[i].key); 48 } 49 } 50 } 51 cout<<dp[number-1].cost<<endl; 52 stack<int>S; //开始竟然用了队列= =、应该用栈,后进先出 53 int temp=number-1; 54 while(dp[temp].pre!=-1){ 55 S.push(dp[temp].key); 56 temp=dp[temp].pre; 57 } 58 while(!S.empty()){ 59 cout<<hw[S.top()].name<<endl; 60 S.pop(); 61 } 62 } 63 return 0; 64 } 65 66 67 68