HDU1074 Doing Homework —— DP

Posted h_z_cong

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU1074 Doing Homework —— DP相关的知识,希望对你有一定的参考价值。

题目链接:http://acm.split.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): 10195    Accepted Submission(s): 4900


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 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <algorithm>
 6 #include <vector>
 7 #include <queue>
 8 #include <stack>
 9 #include <map>
10 #include <string>
11 #include <set>
12 #define ms(a,b) memset((a),(b),sizeof((a)))
13 using namespace std;
14 typedef long long LL;
15 const double EPS = 1e-8;
16 const int INF = 2e9;
17 const LL LNF = 2e18;
18 const int MAXN = 15+10;
19 
20 struct node
21 {
22     string name;
23     int deadline, cost;
24 }subject[MAXN];
25 int dp[1<<16], id[1<<16], pre[1<<16];
26 
27 void Print(int s)
28 {
29     if(!s) return;
30     Print(pre[s]);
31     cout<< subject[id[s]].name <<endl;
32 }
33 
34 int main()
35 {
36     int T, n;
37     scanf("%d", &T);
38     while(T--)
39     {
40         scanf("%d", &n);
41         for(int i = 0; i<n; i++)
42             cin>>subject[i].name>>subject[i].deadline>>subject[i].cost;
43 
44         memset(dp, -1, sizeof(dp));
45         dp[0] = 0;
46         for(int s = 0; s<(1<<n); s++)
47         {
48             int time = 0;
49             for(int i = 0; i<n; i++)
50                 if(s&(1<<i))
51                     time += subject[i].cost;
52 
53             for(int i = 0; i<n; i++)
54             {
55                 if(!(s&(1<<i)))
56                 {
57                     int exceeded = time + subject[i].cost - subject[i].deadline;
58                     if(exceeded<0) exceeded = 0;
59                     int tmp = s|(1<<i);
60                     if(dp[tmp]==-1 || dp[tmp]>dp[s]+exceeded)
61                     {
62                         dp[tmp] = dp[s] + exceeded;
63                         id[tmp] = i;
64                         pre[tmp] = s;
65                     }
66                 }
67             }
68         }
69 
70         printf("%d\n", dp[(1<<n)-1]);
71         Print((1<<n)-1);
72     }
73 }
View Code

 








以上是关于HDU1074 Doing Homework —— DP的主要内容,如果未能解决你的问题,请参考以下文章

Doing Homework HDU - 1074

[2016-03-28][HDU][1074][Doing Homework]

HDU1074 Doing Homework —— DP

[HDU1074]Doing Homework (状压DP)

HDU 1074 Doing Homework

HDU 1074:Doing Homework(状压DP)