【状压DP+输出路径】HDU-1074 Doing Homework
来源:互联网 发布:域名不备案能解析吗 编辑:程序博客网 时间:2024/05/22 11:55
Doing Homework
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
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.
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
23Computer 3 3English 20 1Math 3 23Computer 3 3English 6 3Math 6 3
Sample Output
2ComputerMathEnglish3ComputerEnglishMathHintIn 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这么伤啊
思路:首先要分析题目的数据范围,很多情况下正确的动态规划就是靠分析算法复杂度得到的思路。
只有15种作业。作业最后都是要做完的。而且花费的总时间都是一样的。不同的是,做作业的次序不同,结果就不一样。也就是说仅与时间有关。使用有序集合S来表示当前做作业的状态。
S = {......} --> 决定做第 j 个作业 --> S = {......, j } --> 决定做第k个作业 --> S = {......, j , k }
用一个int来压缩有序集合S。
dp[2^15]表示所有已做作业的状态。接下来就是从这些状态的转移当中搜索到最优解。
首先将当前状态设为INF。(这个状态是未曾计算的)然后枚举当前状态所有已做作业,枚举没做其中一个的时候的状态进行转移。(未计算的当前状态称之为current,已计算的待转移状态称之为before)
显然如果先计算没做字典序最大的那个(这个状态字典序更小的都做过了),那么出现相同解时不应当转移。
如果先计算没做字典序最小的那个(这个状态字典序大的都做了),那么出现相同解时应当转移。
对于应该扣掉的分数应该这样计算:
score = day[before] + work_cost - work_deadline
如果成功转移:day[current] = day[before] + work_cost
P.S. 对于输出路径,只需要借助一个pre数组在发生转移的时候记录一下。
代码如下:
/*ID: j.sure.1PROG:LANG: C++*//****************************************/#include <cstdio>#include <cstdlib>#include <cstring>#include <algorithm>#include <ctime>#include <cmath>#include <stack>#include <queue>#include <vector>#include <map>#include <set>#include <string>#include <climits>#include <iostream>#define For(i,x,y) for(int i = x; i < y; i++)#define Mem(f,x) memset(f, x, sizeof(f))#define Sca(x) scanf("%d", &x)#define LL long longusing namespace std;const int INF = 0x3f3f3f3f;/****************************************/const int N = 1<<15;struct Node {char name[111];int dead, cost;}work[20];int n;int dp[N], day[N], pre[N];void PR(int x){if(!x) return ;PR(x - (1<<pre[x]));printf("%s\n", work[pre[x]].name);}int main(){#ifdef J_Sure//freopen("000.in", "r", stdin);//freopen(".out", "w", stdout);#endifint T;Sca(T);while(T--) {Sca(n);For(i, 0, n) {scanf("%s%d%d", work[i].name, &work[i].dead, &work[i].cost);}int lim = 1 << n;For(i, 1, lim) {dp[i] = INF;for(int j = n-1; j >= 0; j--) {//枚举该状态下最后做的作业int t = 1<<j;if(!(i&t)) continue;int sco = day[i^t] + work[j].cost - work[j].dead;sco = sco < 0 ? 0 : sco;//提前做完是没有加分的if(dp[i] > dp[i^t] + sco) {dp[i] = dp[i^t] + sco;day[i] = day[i^t] + work[j].cost;pre[i] = j;}}}printf("%d\n", dp[lim-1]);PR(lim-1);}return 0;}
0 0
- 【状压DP+输出路径】HDU-1074 Doing Homework
- HDU 1074 Doing Homework(状压DP输出路径)
- HDU 1074 Doing Homework (状压DP + 路径记录)
- HDU 1074 Doing Homework(状压DP+储存路径)
- HDU 1074 Doing Homework(状压DP+记录路径)
- HDU 1074 Doing Homework(状压dp+记录路径)
- hdu 1074 Doing Homework (状压dp)
- hdu 1074 Doing Homework (状压dp)
- HDU 1074 Doing Homework(状压DP)
- HDU 1074 Doing Homework(状压DP)
- HDU 1074-Doing Homework(状压DP)
- HDU 1074Doing Homework 状压Dp
- hdu 1074 Doing Homework 状压dp
- HDU 1074 Doing Homework(状压dp)
- HDU 1074 Doing Homework (状压dp)
- Hdu 1074 Doing Homework 状压dp
- HDU 1074 Doing Homework(状压DP)
- hdu 1074 Doing Homework 状压dp
- Android Sleep和Wait区别
- flex中SDK选择不合适引起的问题
- 数据打包成JSON格式及其解析
- linux下tar.gz、tar、bz2、zip等解压缩、压缩命令小结
- 渗透测试工具Nmap从初级到高级
- 【状压DP+输出路径】HDU-1074 Doing Homework
- 生活——弱者生,强者活
- 【UNIX】UNIX的体系结构
- 从荷兰“范”到希腊“斯” 详解国际足坛各国人名
- Android中插件开发篇之----类加载器
- C# 中的委托和事件--详解(续)
- 关于C#中派生类调用基类构造函数的理解[转]
- Slider 基本属性,
- 盖高辛氏衰,天下归之