问题: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问题的更多相关文章

  1. hoj2662 状态压缩dp

    Pieces Assignment My Tags   (Edit)   Source : zhouguyue   Time limit : 1 sec   Memory limit : 64 M S ...

  2. POJ 3254 Corn Fields(状态压缩DP)

    Corn Fields Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 4739   Accepted: 2506 Descr ...

  3. [知识点]状态压缩DP

    // 此博文为迁移而来,写于2015年7月15日,不代表本人现在的观点与看法.原始地址:http://blog.sina.com.cn/s/blog_6022c4720102w6jf.html 1.前 ...

  4. HDU-4529 郑厂长系列故事——N骑士问题 状态压缩DP

    题意:给定一个合法的八皇后棋盘,现在给定1-10个骑士,问这些骑士不能够相互攻击的拜访方式有多少种. 分析:一开始想着搜索写,发现该题和八皇后不同,八皇后每一行只能够摆放一个棋子,因此搜索收敛的很快, ...

  5. DP大作战—状态压缩dp

    题目描述 阿姆斯特朗回旋加速式阿姆斯特朗炮是一种非常厉害的武器,这种武器可以毁灭自身同行同列两个单位范围内的所有其他单位(其实就是十字型),听起来比红警里面的法国巨炮可是厉害多了.现在,零崎要在地图上 ...

  6. BZOJ-1226 学校食堂Dining 状态压缩DP

    1226: [SDOI2009]学校食堂Dining Time Limit: 10 Sec Memory Limit: 259 MB Submit: 588 Solved: 360 [Submit][ ...

  7. Marriage Ceremonies(状态压缩dp)

     Marriage Ceremonies Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu ...

  8. HDU 1074 (状态压缩DP)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1074 题目大意:有N个作业(N<=15),每个作业需耗时,有一个截止期限.超期多少天就要扣多少 ...

  9. HDU 4511 (AC自动机+状态压缩DP)

    题目链接:  http://acm.hdu.edu.cn/showproblem.php?pid=4511 题目大意:从1走到N,中间可以选择性经过某些点,比如1->N,或1->2-> ...

随机推荐

  1. 传奇的通迅协议与base64算法

    传奇的数据根本就没加密. 只要把#(数字)!去掉,中间的那些全部减0x3c,然后,四个字节一组,每个字节取低六位组成6*4=24位,然后分成3个字节,这个是很经典的base64的算法.不过小改了一些, ...

  2. 15Mybatis_输出类型

    输出类型分为两种:1.resultType          和         2.resultMap 接下来先讲解resultType: 使用resultType进行输出映射,只有查询出来的列名和 ...

  3. DWZ集成的xhEditor编辑器浏览本地图片上传的设置

    有关xhEditor的文件上传配置官方文档链接:http://i.hdu.edu.cn/dcp/dcp/comm/xheditor/demos/demo08.html 一.xhEditor图片上传的配 ...

  4. angularjs中只显示选中的radio的值

    angularjs中,只显示选中的radio的值.主要是相同的radio,name属性值要相同还有ng-model的值要相同,同时要指定value值.这样选中的时候就会在下面的div中显示选中的值了. ...

  5. JQuery 中 is(':visible') 解析及用法

    实例 选择 <body> 元素中每个可见的元素: $("body :visible") 亲自试一试 定义和用法 :visible 选择器选取每个当前是可见的元素. 除以 ...

  6. 史上最全Html与CSS布局技巧

    单列布局水平居中水平居中的页面布局中最为常见的一种布局形式,多出现于标题,以及内容区域的组织形式,下面介绍四种实现水平居中的方法(注:下面各个实例中实现的是child元素的对齐操作,child元素的父 ...

  7. [转]的C#实现三维数字地形漫游(基于Irrlicht)

    马省轩  任丽娜 摘  要:本文采用C#编程语言,利用Irrlicht三维图形引擎实现了三维数字地形的漫游.为三维数字地形显示提供了较易实现的解决方案. 关键词:C#   高度图 Irrlicht引擎 ...

  8. js copy

    Javascript 实现复制(Copy)动作方法大全 投稿:hebedich 字体:[增加 减小] 类型:转载 时间:2014-06-20我要评论 现在浏览器种类也越来越多,诸如 IE.Firefo ...

  9. 优化Hibernate所鼓励的7大措施

    优化Hibernate所鼓励的7大措施: 1.尽量使用many-to-one,避免使用单项one-to-many2.灵活使用单向one-to-many3.不用一对一,使用多对一代替一对一4.配置对象缓 ...

  10. swifter技巧(100)

    一.swift新元素 Tip1:柯里化 将方法进行柯里化,把接受多个参数的方法变换成接受第一个参数的方法,并且返回接受余下的参数,返回结果的新方法. func addTwoNumbers(a: Int ...