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时,还要问几次才能区分出来 若 ...
随机推荐
- 高速入门:十分钟学会Python
初试牛刀 如果你希望学习Python这门语言.却苦于找不到一个简短而全面的新手教程.那么本教程将花费十分钟的时间带你走入Python的大门.本文的内容介于教程(Toturial)和速查手冊(Cheat ...
- linux下常用FTP命令 1. 连接ftp服务器
1. 连接ftp服务器 格式:ftp [hostname| ip-address] a)在linux命令行下输入: ftp 192.168.1.1 b)服务器询问你用户名和密码,分别输入用户名和相应密 ...
- openal资料转贴
地址:http://blog.sina.com.cn/s/blog_685b5b220100ukbp.html OpenAL简介 OpenAL(Open Audio Library)是专门负责3D定位 ...
- 文件名中含有连续字符abc,相应文件中也含有字符串abc
find ./ -name '*abc*' -exec grep 'abc' {} -H \; find ./ -name '*abc*' | xargs -I '{}' grep abc {} -H ...
- 重载 CreateParams 方法[1]: 从一个例子开始(取消窗口最大化、最小化按钮的三种方法)
方法1: 使用 TForm 的 BorderIcons 属性 unit Unit1; interface uses Windows, Messages, SysUtils, Variants, C ...
- C#一个关于委托和事件通俗易懂的例子
using System; namespace Test { public class 室友 { public delegate void 这是一个委托(); public void 起床晨跑去() ...
- 了解一下Windows Cracker
Windows Cracker 消息拆析宏 可以为消息进行参数分解 无需记住或查阅资料来了解WParam和lParam的意义 可以忘记旧的消息处理方式:switch/case 不适合于大型复杂的需要处 ...
- socket小实例
服务端 using System; using System.Collections.Generic; using System.Linq; using System.Text; using Syst ...
- [转]ASP.NET MVC 5 - 创建连接字符串(Connection String)并使用SQL Server LocalDB
您创建的MovieDBContext类负责处理连接到数据库,并将Movie对象映射到数据库记录的任务中.你可能会问一个问题,如何指定它将连接到数据库? 实际上,确实没有指定要使用的数据库,Entity ...
- Linux下安装配置SVN
1.检查系统上是否安装了SVN rpm -qa subversion 没有安装,则使用以下命令安装 yum -y install subversion 2.配置svn并启动svn服务 (1) 指定s ...