HDU 1074 Doing Homework 状压DP

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.
题意:给定n门课目,要按一定顺序完成它们,还给出了完成某课目所需的时间和deadline,超出这门的deadline再完成会按 1 分/天 减去学分,试问最小扣除的学分以及完成顺序。
思路:由于给定的课目数目只有n<=15,且每门课目的状态只有补/未补两个状态,(2^15 = 32768)因此可以使用状态压缩,不过感觉这题的思路更适合使用贪心的策略,也更加简单。
最后要记录转移过程。
(如:11001 表示第1、4、5门课目被完成_(:3」∠)_)
 1 #include <iostream>
 2 #include <string.h>
 3 #include <algorithm>
 4 #include <stdio.h>
 5 using namespace std;
 6 const int INF = 0x3f3f3f3f;
 7 struct sion
 8 {
 9     int sco, cos;
10 }dp[1<<15 + 1];
11 
12 int dead[20], cost[20], prel[1<<15 + 1], pren[1<<15 + 1];
13 char name[20][20];
14 
15 int main()
16 {
17     int T;
18     int n;
19     int t;
20     while(cin >> T)
21     {
22         while(T--)
23         {
24             scanf("%d", &n);
25             for(int i = 0; i < n; i++)
26             {
27                 scanf("%s", name[i]);
28                 cin >> dead[i] >> cost[i];
29             }
30             memset(dp, 0, sizeof(dp));
31             memset(prel, 0, sizeof(prel));
32             memset(pren, 0, sizeof(pren));
33             //////状态压缩
34             int cnt = 0;
35             for(int i = 1; i < (1<<n); i++)
36             {
37                 dp[i].sco = INF;  //初始化
38                 for(int j = n - 1; j >= 0; j--)  //从最后一门课开始判断状态,同时保证无后序性
39                 {
40                     int x = 1 << j;
41                     if(x & i)                    //判断 是否选了第j课
42                     {
43                         if(dp[i - x].cos + cost[j] - dead[j] < 0)   //如果j前面选的课花费的时间小于dead line那么没有减分
44                             t = 0;
45                         else t = dp[i - x].cos + cost[j] - dead[j];
46 
47                         if(t + dp[i-x].sco < dp[i].sco)            //转移
48                         {
49                             dp[i].sco = t + dp[i-x].sco;
50                             dp[i].cos = cost[j] + dp[i-x].cos;     //由于是压缩过的特殊数组,表达某门课是否被选不能直接使用i的值
51                             pren[i] = j;                           //故,记录选取的位置
52                             prel[i] = i - x;                       //记录选取时的值
53                         }
54                     }
55                 }
56             }
57             printf("%d\n", dp[(1<<n) -1].sco);
58             int t = (1<<n) - 1;     //最后转移的课,同时最先的一门课
59             while(t)                //逆序取出
60             {
61                 dead[cnt++]=pren[t];
62                 t = prel[t];
63             }
64             for(int i = cnt-1; i >= 0; i--)
65             {
66                 printf("%s\n", name[dead[i]]);
67             }
68         }
69     }
70 }
71 /**
72   * 
73   *还是太鶸 orz orz orz
74   */

 

posted @ 2016-05-01 22:12  Lweleth  阅读(221)  评论(1编辑  收藏  举报