Doing Homework---hdu1074(状态压缩&&记忆化搜索)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1074
#include <cstdio>
#include <cstring>
#include <iostream>
#include <cmath>
#include <vector>
#include <algorithm>
#include <string>
using namespace std; #define N 1050
#define MOD 1000000007
#define met(a, b) memset(a, b, sizeof(a))
#define INF 0x3f3f3f3f typedef long long LL; int n, pre[N]; struct course
{
char name[];
int deadline, use_time;
} Course[]; struct Status
{
int scores;///当前状态能扣除的最少分数;
int times;///当前状态所需的总时间;
} dp[<<]; void solve(Status& A, Status B, course C)///更新dp;
{
int Times = B.times + C.use_time; int Score = B.scores + max(Times - C.deadline, ); if( A.scores > Score || ( A.scores == Score && A.times > Times ))
{
A.scores = Score;
A.times = Times;
}
} Status dfs(int sta)
{
if( dp[sta].scores != - )///记忆化搜索就是防止一个状态被重复搜索,所以要更新dp的值;
return dp[sta]; dp[sta].scores = INF; for(int i=; i<n; i++)
{
if( sta & (<<i) )///表示当前状态第i门课已经完成了。所以我们要找达到这种状态的其他状态;
solve(dp[sta], dfs(sta-(<<i)), Course[i]);///sta的状态是由sta-(1<<i)加上第i门课得到的;
}
return dp[sta];
} bool Judge(Status A, Status B, course C)
{
int Times = B.times + C.use_time; int Score = B.scores + max(Times - C.deadline, ); return A.scores == Score && A.times == Times;
} void Puts_Path(int sta)
{
if(sta == ) return ; for(int i=n-; i>=; i--)
{
if( sta&(<<i) && Judge(dp[sta], dp[sta-(<<i)], Course[i]) )///判断当前的状态dp[sta]值是不是由dp[sta-(1<<i)]和Course得到;
{
Puts_Path( sta-(<<i) );
printf("%s\n", Course[i].name);///路径是倒着找的的所以应在回溯的过程输出
break;
}
} } int main()
{
int T; scanf("%d", &T); while(T--)
{
met(dp, -); met(Course, ); scanf("%d", &n); for(int i=; i<n; i++)
scanf("%s %d %d", Course[i].name, &Course[i].deadline, &Course[i].use_time); dp[].scores = dp[].times = ; Status ans = dfs( (<<n) - );///从最后一个状态开始找; printf("%d\n", ans.scores); Puts_Path( (<<n) - );///输出路径;
}
return ;
}
记忆化搜索状态压缩
#include <cstdio>
#include <cstring>
#include <iostream>
#include <cmath>
#include <vector>
#include <algorithm>
#include <string>
using namespace std; #define N 16
#define MOD 1000000007
#define met(a, b) memset(a, b, sizeof(a))
#define INF 0x3f3f3f3f typedef long long LL; int n, Path[<<N], Limit; struct course
{
char name[];
int deadline, use_time;
} Course[]; struct Status
{
int scores;///当前状态能扣除的最少分数;
int times;///当前状态所需的总时间;
} dp[<<]; void Puts_Path(int sta)
{
if(sta == ) return ; Puts_Path( sta - (<<Path[sta]) ); printf("%s\n", Course[Path[sta]].name);
} int main()
{
int T; scanf("%d", &T); while(T--)
{
met(dp, INF); met(Course, ); met(Path, -); scanf("%d", &n); for(int i=; i<n; i++)
scanf("%s %d %d", Course[i].name, &Course[i].deadline, &Course[i].use_time); dp[].scores = dp[].times = ; Limit = (<<n) - ; for(int i=; i<=Limit; i++)
{
for(int j=n-; j>=; j--)
{
if( i&(<<j) )continue; int Time = dp[i].times + Course[j].use_time; int Score = dp[i].scores + max(Time - Course[j].deadline, ); int t = i + (<<j); if(dp[t].scores > Score)
{
dp[t].scores = Score;
dp[t].times = Time; Path[t] = j;///状态t是由状态1<<j + j得到的;
}
}
} printf("%d\n", dp[Limit].scores); Puts_Path( Limit );///输出路径,只能从最终状态进行找;
}
return ;
}
普通搜索状态压缩
Doing Homework---hdu1074(状态压缩&&记忆化搜索)的更多相关文章
- loj 1011(状态压缩+记忆化搜索)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=25837 思路:状态压缩+记忆化搜索. #include<io ...
- HDU 4628 Pieces(状态压缩+记忆化搜索)
http://acm.hdu.edu.cn/showproblem.php?pid=4628 题意:给个字符窜,每步都可以删除一个字符窜,问最少用多少步可以删除一个字符窜分析:状态压缩+记忆化搜索 ...
- ACM学习历程—ZOJ3471 Most Powerful(dp && 状态压缩 && 记忆化搜索 && 位运算)
Description Recently, researchers on Mars have discovered N powerful atoms. All of them are differen ...
- GYM 101933E 状态压缩 + 记忆化搜索
题意:我方有n个士兵,敌方有m个,每方士兵都有一个血量,现在有k轮无差别炮火打击,每次都会从存活的士兵中随机选一人,这名士兵的HP就-1,问对方被团灭的概率有多大? 思路:因为n和m的范围很小,我们可 ...
- light oj 1011 - Marriage Ceremonies (状态压缩+记忆化搜索)
题目链接 大概题意是有n个男的n个女的(原谅我这么说,我是粗人),给你一个n*n的矩阵,第i行第j列表示第i个女(男)对第j个男(女)的好感度,然后要安排n对相亲,保证都是正常的(无搞基百合之类的), ...
- ACM-ICPC 2018 徐州赛区网络预赛 B BE, GE or NE(博弈,记忆化搜索)
链接https://nanti.jisuanke.com/t/31454 思路 开始没读懂题,也没注意看数据范围(1000*200的状态,记忆化搜索随便搞) 用记忆化搜索处理出来每个状态的胜负情况 因 ...
- hihoCoder-1087 Hamiltonian Cycle (记忆化搜索)
描述 Given a directed graph containing n vertice (numbered from 1 to n) and m edges. Can you tell us h ...
- 【bzoj5123】[Lydsy12月赛]线段树的匹配 树形dp+记忆化搜索
题目描述 求一棵 $[1,n]$ 的线段树的最大匹配数目与方案数. $n\le 10^{18}$ 题解 树形dp+记忆化搜索 设 $f[l][r]$ 表示根节点为 $[l,r]$ 的线段树,匹配选择根 ...
- 状压DP+记忆化搜索 UVA 1252 Twenty Questions
题目传送门 /* 题意:给出一系列的01字符串,问最少要问几个问题(列)能把它们区分出来 状态DP+记忆化搜索:dp[s1][s2]表示问题集合为s1.答案对错集合为s2时,还要问几次才能区分出来 若 ...
随机推荐
- CentOS下 Uptime 命令
对于一些人来说系统运行了多久是无关紧要的,但是对于服务器管理员来说,这是相当重要的信息.服务器在运行重要应用的时候,必须尽量保证长时间的稳定运行,有时候甚至要求零宕机.那么我们怎么才能知道服务器运行了 ...
- JavaScript DOM 第3章 DOM
3.4.5 获取元素 1. getElementById DOM提供了一个名为getElementById的方法,这个方法将返回一个与那个有着给定的id属性值的元素节点对应的对象. 文档中的每个元素都 ...
- J2SE基础:8.系统经常使用类二
1:基础数据与封装类型之间的转型 A:基础数据类型--->封装类型(对象类型) Boolean boolean_1 = new Boolean(true); byte ---->Byte ...
- mac Virtualbox Ubuntu 设置共享目录
如果要用VirtualBox自带的共享文件夹功能,必须先安装Guest Additions.安装方法:置顶的菜单条->devices->Install Guest Additions.点击 ...
- sip 认证分析
转自:http://blog.csdn.net/wangqi0079/article/details/11569489 SIP类似Http协议.其认证模式也一样.Http协议(RFC 2616 )规定 ...
- [python每日一库]——hotshot
High performance logging profiler 官方文档:http://docs.python.org/2/library/hotshot.html#module-hotshot ...
- 彩色图像的直方图均衡化matlab代码
彩色图像的直方图均衡化 - YangYudong2014的专栏 - CSDN博客 http://blog.csdn.net/yangyudong2014/article/details/4051503 ...
- jQuery弹出层效果
<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head><meta ...
- NHibernate连接oracle报错
NHibernate.Exceptions.GenericADOException:“could not execute query [ select sys_user0_.USERID as USE ...
- python2.0_s12_day9_事件驱动编程&异步IO
论事件驱动与异步IO 事件驱动编程是一种编程范式,这里程序的执行流由外部事件来决定.它的特点是包含一个事件循环,当外部事件发生时使用回调机制来触发相应的处理.另外两种常见的编程范式是(单线程)同步以及 ...