题目链接

 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. easyui的浮动panel不跟随所在页面一起滚动的问题

    项目开发中遇到一个很奇怪的现象就是:随便点开一个下拉控件,包括combo,combobox,databox,combogird等等,都会出现点开的panel面板正常,如果页面有滚动条,一用鼠标滚轮滚动 ...

  2. Feature Extractor[ResNet v2]

    0. 背景 何凯明大神等人在提出了ResNet网络结构之后,对其做了进一步的分析工作,详细的分析了ResNet 构建块能起作用的本质所在.并通过一系列的实验来验证恒等映射的重要性,并由此提出了新的构建 ...

  3. Java消息中间件入门笔记 - ActiveMQ篇

    入门 消息中间件带来的好处: 1)解耦:系统解耦 2)异步:异步执行 3)横向扩展 4)安全可靠 5)顺序保证 栗子: 通过服务调用让其它系统感知事件发生 系统之间高耦合 程序执行效率低 通过消息中间 ...

  4. .net core实践系列之SSO-同域实现

    前言 SSO的系列还是以.Net Core作为实践例子与大家分享,SSO在Web方面复杂度分同域与跨域.本篇先分享同域的设计与实现,跨域将在下篇与大家分享. 如有需要调试demo的,可把SSO项目部署 ...

  5. React中嵌套组件与被嵌套组件的通信

    前言 在React项目的开发中经常会遇到这样一个场景:嵌套组件与被嵌套组件的通信. 比如Tab组件啊,或者下拉框组件. 场景 这里应用一个最简单的Tab组件来呈现这个场景. import React, ...

  6. [WPF]如何使用代码创建DataTemplate(或者ControlTemplate)

    1. 前言 上一篇文章([UWP]如何使用代码创建DataTemplate(或者ControlTemplate))介绍了在UWP上的情况,这篇文章再稍微介绍在WPF上如何实现. 2. 使用Framew ...

  7. require('./expample.js).default详解

    最近总碰到类似于 var a = require('./expample.js).default 这样的代码,感觉很奇葩,总结一波. 为什么会出现这个问题? import 是静态编译的,而 requi ...

  8. SQL Server 跨服务器查询

    select * from OPENDATASOURCE( 'SQLOLEDB', 'Data Source=10.10.10.10;User ID=sa;Password=123456'). [CF ...

  9. 使用PHPExcel导出数据库表结构及内容

    导出表结构: mysql> desc user ; +----------+--------------+------+-----+---------------------+--------- ...

  10. 配置react-sass

    在配置react-sass时遇到很多坑其中 一条如果你的.scss文件失效 请一定要在fileloader之前配置该sass-loader 配置文件如下 基于你不熟悉webpack 容易出这个错误