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时,还要问几次才能区分出来 若 ...
随机推荐
- js中以键值对的形式当枚举
js中以键值对的形式当枚举var Penum= { B: "姓名", C: "所属居委", D: "证件号", E: "性别&qu ...
- mysql分组取每组大的记录
SELECT a.* FROM chat_log a INNER JOIN (SELECT MAX(id) id,to_user FROM chat_log GROUP BY to_user)b ON ...
- linux环境中,nginx安装过程
需求描述: 记录在linux平台,nginx安装的过程. 环境描述: 操作系统:Red Hat Enterprise Linux Server release 6.6 (Santiago) 操作内核版 ...
- redisTools-IdGenerator
public class IdGenerator : RedisToolBase { //redis客户端对象 private static readonly NedisClient client = ...
- MVC--布局--razor
ASP.NET MVC Razor视图引擎攻略 转自:http://www.cnblogs.com/John-Connor/archive/2012/05/08/2487200.html --引子 看 ...
- 淘宝cnpm(可替代nodejs默认npm)
淘宝 NPM 镜像 这是一个完整 npmjs.org 镜像,你可以用此代替官方版本(只读),同步频率目前为 10分钟 一次以保证尽量与官方服务同步. https://npm.taobao.org/
- html5 file 自定义文件过滤
使用 acctpe属性即可 示例: gif,jpg <input type="file" name="pic" accept="image/gi ...
- docker学习-运行第一个docker镜像hello world
docker pull [OPTIONS] NAME[:TAG]:从远程仓库拉取一个镜像到本地,NAME是要拉取的镜像的名称,TAG是docker镜像的版本,不指定的话默认是最新版本 docker ...
- 说说SPI协议
SPI,是英语Serial Peripheral Interface 的缩写,顾名思义就是串行外围设备接口.SPI,是一种高速的,全双工,同步的通信总线,并且在芯片的管脚上只占用四根线,节约了芯片的管 ...
- ARM、MCU、DSP、FPGA、SOC各是什么?区别是什么?(转)
ARM ARM处理器是Acorn计算机有限公司面向低预算市场设计的第一款RISC微处理器.更早称作Acorn RISC Machine.ARM处理器本身是32位设计,但也配备16位指令集,一般来讲比等 ...