hdu 1074 doing homework(状态压缩dp)
2016-02-27 15:28
483 查看
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.
状态压缩DP,我们可以使用一个二进制的数来表示做作业的状态,1表示做了,0表示没做
dp[i]表示状态i损失的分数,再做一个作业x可到另一状态dp[j],则要有i&x==0,若要有dp[a],dp[b]均可以到达dp[j],那么选择损失分数最小的,若分数相同,按题意的字典序,即选择a和b中较小的一个,因为题目的课程是按字典序给出的。
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.
状态压缩DP,我们可以使用一个二进制的数来表示做作业的状态,1表示做了,0表示没做
dp[i]表示状态i损失的分数,再做一个作业x可到另一状态dp[j],则要有i&x==0,若要有dp[a],dp[b]均可以到达dp[j],那么选择损失分数最小的,若分数相同,按题意的字典序,即选择a和b中较小的一个,因为题目的课程是按字典序给出的。
#include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> #include<algorithm> using namespace std; struct node { int time, pre, cost; }dp[1<<15]; struct Node { int deadline, work; char subject[200]; }a[15]; void fun(int t) { int i, num=0; i = dp[t].pre^t; i >>= 1; while (i) { num++; i >>= 1; } if (dp[t].pre) fun(dp[t].pre); printf("%s\n", a[num].subject); } int main() { int t, n, i, flag[1 << 15], j, k, cur, temp; cin >> t; while (t--) { cin >> n; for (i = 0;i < n;i++) cin >> a[i].subject >> a[i].deadline >> a[i].work; int t1 = (1 << n) - 1; memset(flag, 0, sizeof(flag)); dp[0].time = 0, dp[0].pre = -1, dp[0].cost = 0, flag[0] = 1; for (i = 0;i < t1;i++) { for (j = 0;j < n;j++) { cur = 1 << j; if ((cur&i) == 0) { k = cur | i; dp[k].time = dp[i].time + a[j].work; temp = dp[k].time - a[j].deadline; if (temp < 0) temp = 0; temp += dp[i].cost; if (flag[k]) { if (temp < dp[k].cost) { dp[k].cost = temp; dp[k].pre = i; } else if (temp == dp[k].cost) { if (dp[k].pre>i) dp[k].pre = i; } } else { flag[k] = 1, dp[k].cost = temp, dp[k].pre = i; } } } } cout << dp[t1].cost << endl; fun(t1); } return 0; }
相关文章推荐
- hdoj Ipad,IPhone 3802 (矩阵连乘) 好题
- Windows性能指标分析
- vim替换末尾列中的元素
- 2016年博客收集
- inkscape将png图片转换为svg格式的矢量图
- HTML5 HybridApp开发上手指引
- textarea如何实现高度自适应(不出现滚动条)?
- 用Java编写金字塔类型的结构
- Ulua_toLua_基本案例(四)_AccessingLuaVariables
- HDOJ-1016 Prime Ring Problem 素数环 DFS
- 004_Http之response响应头-01重定向
- EasyMonkeyDevice vs MonkeyDevice&HierarchyViewer API Mapping Matrix
- 使用Dmitry Sklyarov的方法来破解一款流行的4G调制解调器
- Leetcode problem 4:Median of Two Sorted Arrays
- android 仿nice实现在图片上打标签
- Android Fragment 生命周期图
- Python——异常编码细节
- C语言 指向结构体变量的指针
- bat文件编写(无线承载网络设置)
- 计算机工作的进行