HDU 1074 Doing Homework
第一次做这道题大概是半个月前了吧,状压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的更多相关文章
- 【状态DP】 HDU 1074 Doing Homework
原题直通车:HDU 1074 Doing Homework 题意:有n门功课需要完成,每一门功课都有时间期限t.完成需要的时间d,如果完成的时间走出时间限制,就会被减 (d-t)个学分.问:按怎样 ...
- HDU 1074 Doing Homework (动态规划,位运算)
HDU 1074 Doing Homework (动态规划,位运算) Description Ignatius has just come back school from the 30th ACM/ ...
- HDU 1074 Doing Homework (dp+状态压缩)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1074 题目大意:学生要完成各科作业, 给出各科老师给出交作业的期限和学生完成该科所需时间, 如果逾期一 ...
- HDU 1074 Doing Homework(状压DP)
第一次写博客ORZ…… http://acm.split.hdu.edu.cn/showproblem.php?pid=1074 http://acm.hdu.edu.cn/showproblem.p ...
- HDU 1074 Doing Homework【状态压缩DP】
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1074 题意: 给定作业截止时间和完成作业所需时间,比截止时间晚一天扣一分,问如何安排作业的顺序使得最 ...
- 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 ...
- HDU 1074 Doing Homework (状态压缩DP)
Doing Homework Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...
- HDU 1074 Doing Homework【状压DP】
Doing Homework Problem Description Ignatius has just come back school from the 30th ACM/ICPC. Now he ...
- HDU 1074 Doing Homework(经典状压dp)
题目链接 Doing Homework Ignatius has just come back school from the 30th ACM/ICPC. Now he has a ...
- HDU 1074 Doing Homework 状压dp(第一道入门题)
Doing Homework Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...
随机推荐
- Android fill_parent、wrap_content和match_parent的区别
三个属性都用来适应视图的水平或垂直大小,一个以视图的内容或尺寸为基础的布局比精确地指定视图范围更加方便. 1)fill_parent 设置一个构件的布局为fill_parent将强制性地使构件扩展,以 ...
- ExtJs之addManagedListener
<!DOCTYPE html> <html> <head> <title>ExtJs</title> <meta http-equiv ...
- dom对象详解--document对象(三)
form对象 form对象代表一个HTML表单,在HTML文档中<form>每出现一次,form对象就会被创建.从dom对象层次图看,document.forms对象是当前文档所有for ...
- MyEclipse 2015 CI
系统 win8.1 MyEclipse 2015 CI 激活后可用 激活工具 地址:http://download.csdn.net/detail/trep10000/8305577
- 关于CStdioFile的使用问题
在win32控制台调试如下程序 #include "stdafx.h"#include <afx.h>//#include <iostream>//usin ...
- socket异步编程--libevent的使用
使用 libevent 和 libev 提高网络应用性能 http://www.ibm.com/developerworks/cn/aix/library/au-libev/ libevent实现ht ...
- 修改bigbluebutton白板上传中文乱码
中文命名的文档,上传是乱码 -- 显示的 打开后, 中文部分是乱码 Comment 1 by project member ffdixon, Nov 08, 2010 Translatio ...
- php ++a和a++
<?php$a=$b=5;$a+$b=$a++-++$b;echo $b;?> 输出-1
- Spring中 @Autowired注解与@Resource注解的区别
Spring中 @Autowired注解与@Resource注解的区别在Spring 3.X中经常使用到@Autowired和@Resource进行装配.这两个注解的差异在何处???相同点:@Reso ...
- FastDFS之java客户端使用
为了方便应用程序的访问FastDFS,官网提供了fastdfs-client-java,以便更好的与应用程序结合使用. 下载fastdfs-client-java源码添加到项目工程里面,添加配置文件: ...