第一次做这道题大概是半个月前了吧,状压DP一个很新鲜的名词

当时看题解怎么也看不懂,现在看懂了以后还是很简单的

所谓状态压缩就是用一个整数的二进制来表示一个状态,比如有三个作业

000表示一科作业也没做,001表示只做了第一科,111表示三科作业都做了

那么从状态0开始出发,遍历每一个状态,如果对于状态S有第i科作业没写那么计算该状态下做完第i科作业对应的扣分数,如果比别人状态下转移过来的扣分数要少,那么状态S就是下一个状态的前驱

用结构体里的pre来保存路径

最后输出科目的时候用递归输出,也是一种很常用的方法

 //#define LOCAL
#include <cstdio>
#include <cstring> const int maxn = ( << );
struct Node
{
int used;
int pre;
int reduced;
}dp[maxn]; struct Course
{
int deadline;
int cost;
char name[];
}course[]; bool vis[maxn]; void OutPut(int S)
{
int t = S ^ dp[S].pre;
int i = -;
while(t)
{
t >>= ;
++i;
}
if(dp[S].pre != )
OutPut(dp[S].pre);
printf("%s\n", course[i].name);
} int main(void)
{
#ifdef LOCAL
freopen("1074in.txt", "r", stdin);
#endif int T;
scanf("%d", &T);
while(T--)
{
int n;
scanf("%d", &n);
for(int i = ; i < n; i++)
scanf("%s%d%d", course[i].name, &course[i].deadline, &course[i].cost);
memset(vis, false, sizeof(vis));
vis[] = true;
dp[].used = , dp[].pre = -, dp[].reduced = ;
int All = ( << n) - ;
for(int S = ; S < All; ++S)
for(int i = ; i < n; ++i)
{
if((S & ( << i)) == )
{//S状态下第i项作业还没做
Node temp;
int next = S | ( << i);
temp.used = dp[S].used + course[i].cost;
temp.pre = S;
temp.reduced = temp.used - course[i].deadline;
if(temp.reduced < ) temp.reduced = ;
temp.reduced += dp[S].reduced; if(vis[next] && temp.reduced < dp[next].reduced)
dp[next] = temp;
else if(!vis[next])
{
vis[next] = true;
dp[next] = temp;
}
}
} printf("%d\n", dp[All].reduced);
OutPut(All);
}
return ;
}

代码君

HDU 1074 Doing Homework的更多相关文章

  1. 【状态DP】 HDU 1074 Doing Homework

    原题直通车:HDU  1074  Doing Homework 题意:有n门功课需要完成,每一门功课都有时间期限t.完成需要的时间d,如果完成的时间走出时间限制,就会被减 (d-t)个学分.问:按怎样 ...

  2. HDU 1074 Doing Homework (动态规划,位运算)

    HDU 1074 Doing Homework (动态规划,位运算) Description Ignatius has just come back school from the 30th ACM/ ...

  3. HDU 1074 Doing Homework (dp+状态压缩)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1074 题目大意:学生要完成各科作业, 给出各科老师给出交作业的期限和学生完成该科所需时间, 如果逾期一 ...

  4. HDU 1074 Doing Homework(状压DP)

    第一次写博客ORZ…… http://acm.split.hdu.edu.cn/showproblem.php?pid=1074 http://acm.hdu.edu.cn/showproblem.p ...

  5. HDU 1074 Doing Homework【状态压缩DP】

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1074 题意: 给定作业截止时间和完成作业所需时间,比截止时间晚一天扣一分,问如何安排作业的顺序使得最 ...

  6. HDU 1074 Doing Homework(像缩进DP)

    Problem Description Ignatius has just come back school from the 30th ACM/ICPC. Now he has a lot of h ...

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

    Doing Homework Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)To ...

  8. HDU 1074 Doing Homework【状压DP】

    Doing Homework Problem Description Ignatius has just come back school from the 30th ACM/ICPC. Now he ...

  9. HDU 1074 Doing Homework(经典状压dp)

    题目链接  Doing Homework        Ignatius has just come back school from the 30th ACM/ICPC. Now he has a ...

  10. HDU 1074 Doing Homework 状压dp(第一道入门题)

    Doing Homework Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)To ...

随机推荐

  1. Sqli-labs less 18

    Less-18 本关我们这里从源代码直接了解到 对uname和passwd进行了check_input()函数的处理,所以我们在输入uname和passwd上进行注入是不行的,但是在代码中,我们看到了 ...

  2. C#中反射泛型 CreateInstance

    假设1我有个类叫SortClass,类中有个BubbleSort(int[] array)用于给数组进行排序. 假设2我有个类叫SortT<T>,类中有个BubbleSort(T[] ar ...

  3. mysql 存储过程事务支持回滚

    如图查看表的属性: InnoDB 支持事务. MyISAM 不支持事务,不过性能更优越.

  4. hdu 3886 Final Kichiku “Lanlanshu” 数位DP

    思路: dp[i][j][k]:满足在字符串的j位,前一位数字是k. 代码如下: #include<iostream> #include<cstdio> #include< ...

  5. Eclipse下Python的MySQLdb的安装以及相关问题

    前提是要安装好Python以及eclipse和MySQL的相应版本.本文Python为2.7,MySQL为5.1Eclipse为3.6.2 下载完MySQLdb以后,直接安装即可.在eclipse中启 ...

  6. 微软VSS的超级BUG

    发现问题:今天一个新同事,无意中发现他直接就登录进VSS了,并且还是“admin”用户: 解决问题:于是开始在网上找度娘和谷歌帮忙,真是不查不要紧,一查吓一跳,VSS本身就存在这个bug,并且是一个超 ...

  7. Linux下查看进程和线程

    在linux中查看线程数的三种方法 1.top -H 手册中说:-H : Threads toggle 加上这个选项启动top,top一行显示一个线程.否则,它一行显示一个进程. 2.ps xH 手册 ...

  8. 点击Button后,执行MouseDown的过程(使用Call Stack观察很清楚)

    Form1上放两个按钮Button1和Button2,默认输入焦点是Button1,现在点击Button2,产生WM_LBUTTONDOWN消息 procedure TForm1.Button2Mou ...

  9. fastdfs-client-java工具类封装

    FastDFS是通过StorageClient来执行上传操作的 通过看源码我们知道,FastDFS有两个StorageClient工具类.

  10. TCP三次握手和四次挥手过程及套接字在各个过程中的状态解析

    说起TCP,我们一般都需要知道发起一个tcp连接和终止一个tcp连接是所发生的事情,下边,我将跟大家介绍下tcp的三次握手及四次挥手的过程. TCP三路握手 (1)服务器必须准备好接受外来的连接.这通 ...