题目链接

 Doing Homework       
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
 
题意:有N门功课,分别有deadline(最后期限)和cost(做这门作业要花的时间),在ddl后x天交作业就要扣x分,而且不能同时做两门功课及以上,问怎么安排顺序扣的分最少。
全排列的话就是N!,肯定会TLE。
相对而言N其实很小,那么可以枚举每门功课选不选,那么状态数最多只有2^15个。这样就不会tle了。
状态压缩DP就是一种用二进制来替代枚举的过程(虽说我感觉本质还是暴力枚举
常使用位运算
dp[i|(1<<j)] = max(dp[i|(1<<j)] , dp[i] + sum_cost) 其中sum_cost表示做完这些已选中的科目所需的时间
输出还有一个打印顺序的,就用pre数组来记录就好了。
 
代码如下:
 
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; const int INF = 0x3f3f3f3f;
const int maxn = ;
struct Node {
char name[];
int D,C;
} node[maxn];
int dp[<<maxn], pre[<<maxn];
int n; void print_ans(int now) {
if (!now) return;
int temp;
for (int i = ; i < n; i++) {
if ((now & (<<i)) && !(pre[now] & (<<i))) {
temp = i;
break;
}
}
print_ans(pre[now]);
puts(node[temp].name);
} int main() {
int T; scanf("%d", &T);
while (T--) {
memset(dp, 0x3f, sizeof(dp));
memset(pre, , sizeof(pre));
scanf("%d", &n);
for (int i = ; i < n; i++) scanf("%s%d%d", node[i].name, &node[i].D, &node[i].C);
dp[] = ;
for (int i = ; i < ( << n); i++) {
for (int j = ; j < n; j++) {
if (i & ( << j)) continue;
int s = ;
for (int k = ; k < n; k++)
if (i & ( << k)) s += node[k].C;
s += node[j].C;
if (s > node[j].D) s -= node[j].D;
else s = ;
if (dp[i|(<<j)] > dp[i] + s) {
dp[i|(<<j)] = dp[i] + s;
pre[i|(<<j)] = i;
}
}
}
printf("%d\n", dp[(<<n)-]);
print_ans((<<n)-);
}
}

HDU 1074 Doing Homework(经典状压dp)的更多相关文章

  1. HDU 1074 Doing Homework(状压DP)

    第一次写博客ORZ…… http://acm.split.hdu.edu.cn/showproblem.php?pid=1074 http://acm.hdu.edu.cn/showproblem.p ...

  2. HDU 1074 Doing Homework【状压DP】

    Doing Homework Problem Description Ignatius has just come back school from the 30th ACM/ICPC. Now he ...

  3. HDU 1074 Doing Homework (状压dp)

    题意:给你N(<=15)个作业,每个作业有最晚提交时间与需要做的时间,每次只能做一个作业,每个作业超出最晚提交时间一天扣一分 求出扣的最小分数,并输出做作业的顺序.如果有多个最小分数一样的话,则 ...

  4. HDU 1074:Doing Homework(状压DP)

    http://acm.hdu.edu.cn/showproblem.php?pid=1074 Doing Homework Problem Description Ignatius has just ...

  5. poj3311 TSP经典状压dp(Traveling Saleman Problem)

    题目链接:http://poj.org/problem?id=3311 题意:一个人到一些地方送披萨,要求找到一条路径能够遍历每一个城市后返回出发点,并且路径距离最短.最后输出最短距离即可.注意:每一 ...

  6. HDU 6149 Valley Numer II 状压DP

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6149 题意:中文题目 解法:状压DP,dp[i][j]代表前i个低点,当前高点状态为j的方案数,然后枚 ...

  7. HDU 5434 Peace small elephant 状压dp+矩阵快速幂

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5434 Peace small elephant  Accepts: 38  Submissions: ...

  8. kuangbin专题十二 HDU1074 Doing Homework (状压dp)

    Doing Homework Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)To ...

  9. HDU 4906 Our happy ending (状压DP)

    HDU 4906 Our happy ending pid=4906" style="">题目链接 题意:给定n个数字,每一个数字能够是0-l,要选当中一些数字.然 ...

随机推荐

  1. Generative Adversarial Nets[content]

    0. Introduction 基于纳什平衡,零和游戏,最大最小策略等角度来作为GAN的引言 1. GAN GAN开山之作 图1.1 GAN的判别器和生成器的结构图及loss 2. Condition ...

  2. ODPS-Java-SDK快速入门

    一.简介 核心接口包括:AliyunAccount,MaxCompute(SDK中使用原名ODPS)等常见对象组件 更多参见文档:https://help.aliyun.com/document_de ...

  3. UITableView 和 UITableViewController

    UITableView:显示有多行数据的一个列. 新建一个过程:Xcode -> File -> New -> Project...,然后选择iOS -> Applicatio ...

  4. Bing.com在.NET Core 2.1上运行!

    Bing.com在.NET Core 2.1上运行! 相关知识请参考.netCore开发团队博客(https://blogs.msdn.microsoft.com/dotnet/) Bing.com是 ...

  5. 小P的金字塔

    题目描述 小P感到自己前几天太作了,于是非常有远见的决定为自己建立一座金字塔. 现在他有n种标准长方体的石头,每种石头只有两个,第i种石头是长宽高分别为Xi,Yi,Zi的长方体.由于整个工程只有小P一 ...

  6. HNOI2013 BZOJ3144 切糕

    在n×m的表格上,在(x,y)填v的代价是w(x,y,v),且相邻格子填的数相差≤d.求填满表格的最小代价.n,m,maxv≤40. 每个点上选择一个数填,因此将上面的数串起来.考虑限制条件,矛盾条件 ...

  7. ImageProcessor组件

    ImageProcessor组件 开源免费的.NET图像即时处理的组件ImageProcessor   承接以前的组件系列,这个组件系列旨在介绍.NET相关的组件,让大家可以在项目中有一个更好的选择, ...

  8. CRM系统(第三部分)

      阅读目录 1.销售与客户的表结构 2.公共客户池 3.确认跟进 4.我的客户 5.code 1.销售与客户的表结构 1.公共客户与我的客户 ---公共客户(公共资源) 1.没有报名 2.3天没有跟 ...

  9. (关于数据传输安全)SSH协议

    这里说的不是java的SSH框架,是1995年,芬兰学者Tatu Ylonen设计的SSH协议. 有计算机网络基础的同学都知道,在网上传输的数据是可以被截取的.那么怎样才能获得安全? 一.春点行话 电 ...

  10. php之常用扩展总结

    在此总结,开发中经常使用到的扩展,来进行日常PHP的开发工作 bcmath(精确数值处理) bz2 calendar Core ctype curl date dom ereg exif filein ...