状态压缩dp问题
问题: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种家庭作业,全部做完有n!种做的顺序
但是n!太大了,且对于完成作业1,2,3和1,3,2和2,1,3和3,2,1和3,1,2来说
完成它们消耗的天数一定是一样的,只是完成的顺序不同从而扣的分不同
所以可以将完成相同的作业的所有状态压缩成一种状态并记录扣的最少分即可
即:状态压缩dp
对于到达状态i,从何种状态到达i呢?只需要枚举所有的作业
假如对于作业k,i中含有作业k已完成,那么i可以由和i状态相同的状态仅仅是k未完成的
状态j=i-(1<<k)来完成k到达,并且j一定比i小,如果状态从0枚举到2^n-1那么j一定是在i之前已经计算过的
*/
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <queue>
#include <algorithm>
#include <map>
#include <cmath>
#include <iomanip>
#define INF 99999999
typedef long long LL;
using namespace std;
const int MAX=(1<<15)+10;
int n;
int dp[MAX],t[MAX],pre[MAX],dea[20],fin[20];//dp[i]记录到达状态i扣的最少分,t时相应的花去多少天了
char s[20][110];
void output(int x){
if(!x)return;
output(x-(1<<pre[x]));
printf("%s\n",s[pre[x]]);
}
int main(){
int T;
scanf("%d",&T);
while(T--){
scanf("%d",&n);
for(int i=0;i<n;++i)scanf("%s%d%d",&s[i],&dea[i],&fin[i]);
int bit=1<<n;
for(int i=1;i<bit;++i){//枚举到达状态i
dp[i]=INF;//初始化到达状态i的扣分
for(int j=n-1;j>=0;--j){//由于输入时按字符大小输入,而每次完成j相当于把j放在后面完成且下面判断是dp[i]>dp[i-temp]+score
int temp=1<<j; //所以是n-1开始,如果下面判断是dp[i]>=dp[i-temp]+score则从0开始
if(!(i&temp))continue;//状态i不存在作业j完成则不能通过完成作业j到达状态i
int score=t[i-temp]+fin[j]-dea[j];//i-temp表示没有完成j的那个状态
if(score<0)score=0;//完成j被扣分数最小是0
if(dp[i]>dp[i-temp]+score){
dp[i]=dp[i-temp]+score;
t[i]=t[i-temp]+fin[j];//到达状态i花费的时间
pre[i]=j;//到达状态i的前驱,为了最后输出完成作业的顺序
}
}
}
printf("%d\n",dp[bit-1]);
output(bit-1);//输出完成作业的顺序
}
return 0;
}
状态压缩dp问题的更多相关文章
- hoj2662 状态压缩dp
Pieces Assignment My Tags (Edit) Source : zhouguyue Time limit : 1 sec Memory limit : 64 M S ...
- POJ 3254 Corn Fields(状态压缩DP)
Corn Fields Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 4739 Accepted: 2506 Descr ...
- [知识点]状态压缩DP
// 此博文为迁移而来,写于2015年7月15日,不代表本人现在的观点与看法.原始地址:http://blog.sina.com.cn/s/blog_6022c4720102w6jf.html 1.前 ...
- HDU-4529 郑厂长系列故事——N骑士问题 状态压缩DP
题意:给定一个合法的八皇后棋盘,现在给定1-10个骑士,问这些骑士不能够相互攻击的拜访方式有多少种. 分析:一开始想着搜索写,发现该题和八皇后不同,八皇后每一行只能够摆放一个棋子,因此搜索收敛的很快, ...
- DP大作战—状态压缩dp
题目描述 阿姆斯特朗回旋加速式阿姆斯特朗炮是一种非常厉害的武器,这种武器可以毁灭自身同行同列两个单位范围内的所有其他单位(其实就是十字型),听起来比红警里面的法国巨炮可是厉害多了.现在,零崎要在地图上 ...
- BZOJ-1226 学校食堂Dining 状态压缩DP
1226: [SDOI2009]学校食堂Dining Time Limit: 10 Sec Memory Limit: 259 MB Submit: 588 Solved: 360 [Submit][ ...
- Marriage Ceremonies(状态压缩dp)
Marriage Ceremonies Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu ...
- HDU 1074 (状态压缩DP)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1074 题目大意:有N个作业(N<=15),每个作业需耗时,有一个截止期限.超期多少天就要扣多少 ...
- HDU 4511 (AC自动机+状态压缩DP)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4511 题目大意:从1走到N,中间可以选择性经过某些点,比如1->N,或1->2-> ...
随机推荐
- AutoIT脚本的语法特征
这里主要介绍AutoIT的脚本语法特征,包括变量.关键字.宏.设置选项等,详细的语法细节,可以参考其用户手册,也可以去AutoIT中文论坛(www.autoit.net.cn)交流. 1. 变量 Au ...
- java 21 - 11 IO流的标准输入流和标准输出流
标准输入输出流 System类中的两个成员变量: public static final InputStream in "标准"输入流. public static final P ...
- EasyUI中页面必须刷新才显示tree组件最新数据的BUG解决方案
在URL地址后面加个时间戳,这样就避免从浏览器缓存里读取数据了 $("#devtree").tree({ url: '/Deviceinfo/ModelsTree.aspx?cmd ...
- f2fs解析(十)nid 如何从nat_root中删除
上面我们谈到了一个nid如何从free_nid中转移到node_info中去[分别有一个链表和一棵基数树搭伙做事],讲free_nid时,详细说明了free_nid中是如何进如何出的,上一篇说了nid ...
- 003医疗项目-关于<context:property-placeholder location="classpath:db.properties"/>的问题
项目结构如下:
- C++ 排序函数 sort(),qsort()的用法
转自:http://blog.csdn.net/zzzmmmkkk/article/details/4266888/ 所以自己总结了一下,首先看sort函数见下表: 函数名 功能描述 sort 对给定 ...
- [转]基于四叉树(QuadTree)的LOD地形实现
实现基于四叉树的LOD地形时,我遇到的主要问题是如何修补地形裂缝. 本段我将描述使用LOD地形的优势,我实现LOD地形的思路,实现LOD地形核心模块的详细过程,以及修补地形裂缝的思路. 首先,LOD地 ...
- sql语句or与union all的执行效率比较
看到一篇文章是讲sql语句or与union all的执行效率比较的,以前没怎么注意这个问题,感觉文章写的不错,转来一看. 文章原链接:http://www.cunyoulu.com/zhuanti/q ...
- ReactNative之style使用指南
ReactNative中能使用的css样式有哪些呢Valid style props: [ "alignItems", "alignSelf", & ...
- Android RotateAnimation详解
RotateAnimation旋转坐标系为以旋转点为坐标系(0,0)点.x轴为0度,顺时针方向旋转一定的角度.1.RotateAnimation(fromDegrees, toDegrees) [默认 ...