Problem Description
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

思路:
这题是很值的花时间好好理解的一道题目,当时就是把这题给搞明白了,才对DP有了真正意义上的认识
将所有的状态非常直观的转化成二进制数列,然后分别求”当前状态的最大值“,这一途径是通过遍历所有能构成当前状态的”上一状态“实现的
这样就很直观的展现了DP的思想,即当前的”最优解“是通过枚举所有能达到当前状态的”上一状态“的解来实现。

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<stack>
using namespace std; #define N 15 typedef struct
{
char name[];
int deadline;
int cost;
}Course; typedef struct
{
int timeUsed; //到此状态总共花费的时间
int minPoint; //此状态最小的被罚分数
int preState; //此状态对应的前一状态
int lastCourse; //从上一状态转移到此状态完成的course
}State; Course homework[];
State states[<<N];
int n; //课程数 void outputResult()
{
stack<int> s;
int tempState;
int tempCourse;
char courseName; tempState = (<<n) - ;
printf("%d\n", states[tempState].minPoint);
while(tempState != )
{
tempCourse = states[tempState].lastCourse;
s.push(tempCourse);
tempState = states[tempState].preState;
} while(!s.empty())
{
tempCourse = s.top();
s.pop();
printf("%s\n", homework[tempCourse].name);
}
} void processDP()
{
int i, j, maxStateN;
int temp;
int reduce;
int preState; states[].lastCourse = ;
states[].minPoint = ;
states[].preState = -;
states[].timeUsed = ; maxStateN = <<n; for(i=; i<maxStateN; i++)
{
states[i].minPoint = 0x7fffffff;
for(j=n-; j>=; j--) //这里j从n-1递减到0,是为了保证当罚分一样时,顺序按照字母顺序递增的方式排列
{
temp = <<j;
if(i & temp) //状态i中包含第j门课
{ preState = i - temp;
reduce = states[preState].timeUsed+homework[j].cost-homework[j].deadline;
if(reduce < )
{
reduce = ;
} if(states[i].minPoint > states[preState].minPoint + reduce)
{
states[i].minPoint = states[preState].minPoint + reduce;
states[i].lastCourse = j;
states[i].preState = preState;
states[i].timeUsed = states[preState].timeUsed + homework[j].cost;
}
}
}
}
} int main()
{
int i;
int T; scanf("%d", &T);
while(T--)
{
scanf("%d", &n);
for(i=; i<n; i++)
{
scanf("%s%d%d", homework[i].name, &homework[i].deadline, &homework[i].cost);
} processDP();
outputResult();
}
return ;
}

HDU-1047(DP-二进制状态压缩)的更多相关文章

  1. hdu 1429 bfs+二进制状态压缩

    开始时候只用了BFS,显然超时啊,必然在结构体里加一个数组什么的判重啊,开始用的一个BOOL数组,显然还是不行,复杂度高,每次都要遍历数组来判重:后百度之,学习了二进制状态压缩,其实就用一个二进制数来 ...

  2. HDU 3001 Travelling(状态压缩DP+三进制)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3001 题目大意:有n个城市,m条路,每条路都有一定的花费,可以从任意城市出发,每个城市不能经过两次以上 ...

  3. [BZOJ 4455] [ZJOI 2016] 小星星 (树形dp+容斥原理+状态压缩)

    [BZOJ 4455] [ZJOI 2016] 小星星 (树形dp+容斥原理+状态压缩) 题面 给出一棵树和一个图,点数均为n,问有多少种方法把树的节点标号,使得对于树上的任意两个节点u,v,若树上u ...

  4. POJ 2777.Count Color-线段树(区间染色+区间查询颜色数量二进制状态压缩)-若干年之前的一道题目。。。

    Count Color Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 53312   Accepted: 16050 Des ...

  5. # 最短Hamilton路径(二进制状态压缩)

    最短Hamilton路径(二进制状态压缩) 题目描述:n个点的带权无向图,从0-n-1,求从起点0到终点n-1的最短Hamilton路径(Hamilton路径:从0-n-1不重不漏的每个点恰好进过一次 ...

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

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

  7. hdu 2825(ac自动机+状态压缩dp)

    题意:容易理解... 分析:在做这道题之前我做了hdu 4057,都是同一种类型的题,因为题中给的模式串的个数最多只能为10个,所以我们就很容易想到用状态压缩来做,但是开始的时候我的代码超时了dp时我 ...

  8. HDU 1074 Doing Homework (状态压缩 DP)

    题目大意: 有 n 项作业需要完成,每项作业有上交的期限和需完成的天数,若某项作业晚交一天则扣一分.输入每项作业时包括三部分,作业名称,上交期限,完成所需要的天数.求出完成所有作业时所扣掉的分数最少, ...

  9. Hdu 4778 Gems Fight! (状态压缩 + DP)

    题目链接: Hdu 4778 Gems Fight! 题目描述: 就是有G种颜色,B个背包,每个背包有n个宝石,颜色分别为c1,c2............两个人轮流取背包放到公共容器里面,容器里面有 ...

  10. [HDU 4336] Card Collector (状态压缩概率dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4336 题目大意:有n种卡片,需要吃零食收集,打开零食,出现第i种卡片的概率是p[i],也有可能不出现卡 ...

随机推荐

  1. [Redux] Composition with Objects

    For example, current we have those todos: { todos: [ { completed: true, id: 0, text: "Learn Red ...

  2. 静态代码检查工具 cppcheck 的使用

      CppCheck是一个C/C++代码缺陷静态检查工具.不同于C/C++编译器及其它分析工具,CppCheck只检查编译器检查不出来的bug,不检查语法错误.所谓静态代码检查就是使用一个工具检查我们 ...

  3. Java基础知识强化92:日期工具类的编写和测试案例

    1. DateUtil.java,代码如下: package cn.itcast_04; import java.text.ParseException; import java.text.Simpl ...

  4. 关于centos6.5系统安装FTP服务和配置的方法

    一般在配置服务器的时候,涉及到代码上传,通常都要用到FTP方式. 1.先查看系统是否安装vsftpd: rpm -qa | grep vsftpd 如果出现vsftpd-2.2.2-14......字 ...

  5. Java数据库连接之配置ODBC数据源

    java使用JDBC-ODBC桥接连接SQLServer数据库需要配置ODBC数据源,配置步骤如下: 1.进入控制面板,找到管理工具 2.看到ODBC数据源,有64位和32位的,如果你的数据库是64位 ...

  6. DotNet中的计时器线程计时器

    转载自:http://hi.baidu.com/wingingbob/item/9f1c9615f3b24d5f2b3e225c 基于多线程设计,计时器工作在ThreadPool线程上,存在事件的重入 ...

  7. CentOS目录结构详解

    CentOS是文件管理系统,在CentOS中所有的程序都以文件形式存储.初学CentOS的朋友需要了解各个挂载点 目录的结构和作用.这样才能更好的去管理系统. CentOS的目录大体上可分为四类:管理 ...

  8. android studio 改变主题及字体大小

    一丶修改主题背景 1.点击图中的小扳子图标(设置),或者点击file->setting-> 2.找到Appearance->在Theme中选择Darcula,即可改变成当前最主流的主 ...

  9. Xcode 运行报错:“Your build settings specify a provisioning profile with the UUID ****** however, no such provisioning profile was found”

    iOS开发中遇到"Your build settings specify a provisioning profile with the UUID ****** however, no su ...

  10. if exists用法

    1 判断数据库是否存在Sql代码 if exists (select * from sys.databases where name = ’数据库名’)    drop database [数据库名] ...