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 ...
随机推荐
- Sqli-labs less 36
Less-36 我们直接看到36关的源代码 上面的check_quotes()函数是利用了mysql_real_escape_string()函数进行的过滤. mysql_real_escape_st ...
- Win8必知快捷键汇总
* Win+C:调出应用Charm菜单(开始界面.传统桌面) * Win+D:所有程序最小化,再次按下恢复(开始界面.传统桌面) * Win+E:打开我的电脑(开始界面.传统桌面) * Win+F:调 ...
- 字典树trie的学习与练习题
博客详解: http://www.cnblogs.com/huangxincheng/archive/2012/11/25/2788268.html http://eriol.iteye.com/bl ...
- SQL技术内幕-12 SQL优化方法论前言
我推荐的一种使用自顶向下的优化论.这种方法,首先分析实例级的等待时间,在通过一系列步骤将其不断细化,知道找出系统中导致大量等待的进程/组件.一旦找出这些令人讨厌的进程,就可以集中优化他们了,一下是这种 ...
- 在运行jar时自动加载指定的jar包
初学Java的人经常遇到的一个问题是:如果一个程序依赖某个文件夹下的一堆jar包,那么启动它的时候就需要在java -cp参数后面一个一个的加上jar包的名称,很不方便. 比如主程序类叫Main,在目 ...
- Win32应用程序中文支持
Settings--Editor---Encoding改为Windows 936 main.cpp中#include "locale.h" winmain中增加一行: setloc ...
- POJ 1279 Art Gallery(半平面交求多边形核的面积)
题目链接 题意 : 求一个多边形的核的面积. 思路 : 半平面交求多边形的核,然后在求面积即可. #include <stdio.h> #include <string.h> ...
- IEEE 802.3 Ethernet
Introduction Ethernet 是过去30年以来最为成功的局域网(local area networking)技术. 1. First widely used LAN technology ...
- ring0 与 ring3 层之间的交互
在进行Windows的ring0层开发时,必不可免的要与 ring3 层进行交互.进行数据间的相互传输.可用的方法有DeviceIoCntrol,ReadFile.我平常都是用的DeviceIoCon ...
- JAVA Map集合类简介
了解最常用的集合类型之一 Map 的基础知识以及如何针对您应用程序特有的数据优化 Map. 本文相关下载: · Jack 的 HashMap 测试· Oracle JDeveloper 10g jav ...