题目链接

 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. maven的下载,环境变量配置,和eclipse配置,和IntelliJIDEA配置

    一.maven简介 Maven是一个项目管理工具,它包含了一个项目对象模型 (Project Object Model),一组标准集合,一个项目生命周期(Project Lifecycle),一个依赖 ...

  2. 总结一下linux中的分段机制

    本文为原创,转载请注明:http://www.cnblogs.com/tolimit/ 这篇文章主要说一下linux对于分段机制的处理,虽然都说linux不使用分段机制,但是分段机制属于CPU的一个功 ...

  3. 用包来组织模型 -- Django从入门到精通系列教程

    该系列教程系个人原创,并完整发布在个人官网刘江的博客和教程 所有转载本文者,需在顶部显著位置注明原作者及www.liujiangblog.com官网地址. 在我们使用python manage.py ...

  4. FineUIMvc随笔(2)怎样在控件中嵌套 HTML

    声明:FineUIMvc(基础版)是免费软件,本系列文章适用于基础版. 用户需求 有网友在<FineUI总群1>问这么一个问题:怎么把 HTML 嵌套在控件中? 这是很多刚学习 FineU ...

  5. js实现活动倒计时

    let startTime = 1527647143949; // 开始时间 var time = new Countdown('timer',startTime); function Countdo ...

  6. RabbitMQ总结

    消息队列 三个业务场景:解耦.异步.削峰 带来问题 系统可用性降低:外部依赖越多,越容易挂掉. 系统复杂性提高:重复消费,消息丢失,消息传递的顺序性 一致性问题: 一.如何保证消息的可靠性传输(如何处 ...

  7. 腾讯内推一面C++

    北邮论坛找个腾讯的内推,没想到那么快就安排面试了.第一次面腾讯,写点东西记录一下吧. 面的是位置服务部门. 去了之后HR先给了两张纸,有三道编程题.第一道是求 二进制中1的个数(考察位运算)(剑指of ...

  8. H5 58-网页的布局方式

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. H5 文本属性

    06-文本属性 我是文字 我是文字 我是段落我是段落我是段落我是段落我是段落我是段落我是段落我是段落我是段落我是段落我是段落我是段落我是段落我是段落我是段落我是段落我是段落我是段落我是段落我是段落我是 ...

  10. 自己实现数据结构系列五---BinarySearchTree

    一.二分搜索树: 1.代码: public class BST<E extends Comparable<E>> { private class Node{ public E ...