问题: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. Ajax读取文件时出现的缓存问题

    对于Ajax缓存问题时,由于浏览器的版本问题,有时候当服务器端已更改文件中的内容,而客户端并得不到更新后的文件,而是延续之前的文件内容,解决办法是:在读取的文件内容后加一串的地址:JSON的格式为[{ ...

  2. Jenkins学习八:Jenkins语言本地化

    在Jenkins中,英语一大片,不懂英语的看着头疼.非常高兴的是,Jenkins作为一个主流流行的持续构建工具,提供了一个本地化语言的配置界面. 你可以找到它,在Jenkins每页的左下角.如下图: ...

  3. java 16-6 泛型

    ArrayList存储字符串并遍历 我们按照正常的写法来写这个程序, 结果确出错了. 为什么呢? 因为我们开始存储的时候,存储了String和Integer两种类型的数据. 而在遍历的时候,我们把它们 ...

  4. mysql客户端授权后连接失败问题

    在本地(192.168.1.152)部署好mysql环境,授权远程客户机192.168.1.%连接本机的mysql,在iptables防火墙也已开通3306端口.如下:mysql> select ...

  5. 利用concat进行数组复制

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

  6. 19Mybatis_订单商品数据模型分析

    这篇文章是对订单商品数据模型进行分析(会给出分析思路),有四张表.这篇文章是后续文章的基础,因为后续的文章要针对这个数据模型(四张表)进行一对一,一对多,多对多进行查询. 我们以后会碰到各种各样的数据 ...

  7. sqlSQL2008如何创建定时作业(代理服务)(转)

    SQL2008如何创建定时作业?此方法也适应于Sql Server2005数据库,有兴趣的可以来看下! 1.打开[SQL Server Management Studio],在[对象资源管理器]列表中 ...

  8. CentOS 7 添加win7启动项——修改默认启动项

    CentOS 7使用grub2引导启动,在win7之后装完CentOS再启动会丢失win7启动项. 首先,添加win7启动项,步骤如下: 1.使用root登陆系统 2.用文本编辑器打开 /boot/g ...

  9. 0.1 hint crack

    http://files.cnblogs.com/files/crac/27.rar

  10. php基础19:文件

    <?php //1.打开文件的更好的方法是通过 fopen() 函数.此函数为您提供比 readfile() 函数更多的选项. //fopen() 的第一个参数包含被打开的文件名,第二个参数规定 ...