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下rpm指令和yum指令详解
centos的软件安装大致可以分为两种类型: [centos]rpm文件安装,使用rpm指令 类似[ubuntu]deb文件安装,使用dpkg指令 [centos]yum安装 类似[ubuntu]ap ...
- Vuforia AR实战教程
官网:https://developer.vuforia.com/ Vuforia AR实战教程 http://www.taikr.com/my/course/531. AQaVpF//////AAA ...
- django 关闭debug模式,报500错误
今天关闭了程序的debug模式,结果提示500错误.但是启动debug模式,又运行正常. Server Error (500) 上网查了以后,发现django1.5版本的要设置ALLOWED_HOST ...
- JavaScript编码encode和decode escape和unescape
encodeURI() 函数可把字符串作为 URI 进行编码. 语法 encodeURI(URIstring) 参数 描述 URIstring 必需.一个字符串,含有 URI 或其他要编码的文本. 返 ...
- C#的字符串优化-String.Intern、IsInterned
https://www.jianshu.com/p/af6eb8d3d4bf 首先看一段程序: using System; class Program { static void Main(strin ...
- layui时间,table,大图查看,弹出框,获取音频长度,文件上传
1.引入: <link href="../../Scripts/layui-v2.3.0/css/layui.css" rel="stylesheet" ...
- Python 练习题:计算 MAC 地址
#!/usr/bin/env python #-*- coding:utf-8 -*- ''' 给一个MAC地址加1 ''' mac = '52:54:00:e6:b2:0a' prefix_mac ...
- Triton调试记录
先编译Release版本 先从下拉列表选择Release-MT-DLL,然后选中Triton-vc14工程, 修改项目属性配置为Release-MT-DLL-NODX,NODX的意思是不使用Direc ...
- Hadoop学习之路
Hadoop是谷歌的集群系统的开源实现: -google集群系统:GFS.MapReduce.BigTable -Hadoop主要由HDFS(hadoop distrubuted file syste ...
- option 选不中问题
function appAndBuz(appName,buzName,areaCenterCode){ //appName,buzName下拉框的值start $.ajax({ type: " ...