Engineer Assignment HDU - 6006 状压dp
http://acm.split.hdu.edu.cn/showproblem.php?pid=6006
比赛的时候写了一个暴力,存暴力,过了,还46ms
那个暴力的思路是,预处理can[i][j]表示第i个人能否胜任第j个项目,能胜任的条件就是它和这个项目有共同的需求。
然后暴力枚举每一个人去搭配哪一个项目,
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <string>
#include <stack>
#include <map>
#include <set>
#include <bitset>
#include <cstdlib>
#include <ctime>
#define X first
#define Y second
#define clr(u,v); memset(u,v,sizeof(u));
#define inff() freopen("data","r",stdin);
#define out() freopen("ans","w",stdout);
#define Clear(Q); while (!Q.empty()) Q.pop();
#define pb push_back
using namespace std;
typedef long long ll;
typedef long long LL;
typedef pair <int, int> pii;
const LL INF = 1e17;
const int inf = 0x3f3f3f3f;
const int maxn = 1e2 + ;
vector<int> project[maxn], man[maxn];
int e[maxn][maxn], DFN;
bool can[][];
int n, m; // n_project, m_man
int has[][maxn];
bool need[][maxn];
int ans = ;
void dfs(int cur) {
if (cur == m + ) {
int t = ;
for (int i = ; i <= n; ++i) {
bool flag = true;
for (int j = ; j < project[i].size(); ++j) {
if (!has[i][project[i][j]]) {
flag = false;
break;
}
}
t += flag;
}
ans = max(ans, t);
return;
}
// bool can = false;
for (int i = ; i <= n; ++i) {
if (!can[cur][i]) continue;
bool flag = false;
for (int j = ; j < man[cur].size(); ++j) {
if (!need[i][man[cur][j]]) continue;
if (has[i][man[cur][j]]) continue;
flag = true;
break;
}
if (!flag) continue;
for (int j = ; j < man[cur].size(); ++j) {
has[i][man[cur][j]]++;
}
dfs(cur + );
for (int j = ; j < man[cur].size(); ++j) {
has[i][man[cur][j]]--;
}
}
dfs(cur + );
}
void work() {
memset(can, false, sizeof can);
memset(need, false, sizeof need);
memset(has, false, sizeof has);
scanf("%d%d", &n, &m);
++DFN;
for (int i = ; i <= n; ++i) { // project
int c;
scanf("%d", &c);
project[i].clear();
while (c--) {
int val;
scanf("%d", &val);
project[i].push_back(val);
e[i + m][val] = DFN;
need[i][val] = true;
}
}
for (int i = ; i <= m; ++i) { // man
int c;
scanf("%d", &c);
man[i].clear();
while (c--) {
int val;
scanf("%d", &val);
man[i].push_back(val);
e[i][val] = DFN;
}
}
for (int i = ; i <= m; ++i) { // man
for (int j = ; j <= n; ++j) { // project
for (int k = ; k <= ; ++k) { // major
if (e[i][k] == DFN && e[j + m][k] == DFN) {
can[i][j] = true;
break;
}
}
}
}
ans = ;
dfs();
static int f = ;
printf("Case #%d: %d\n", ++f, ans);
} int main()
{
#ifdef LOCAL
inff();
#endif
int T;scanf("%d",&T);
while(T--)
{
work();
}
return ;
}
正解是状压dp
其实是一个挺好想的dp
dp[i][1 << m]表示处理了前i个项目,状态是j的时候的最大完成数目。
首先预处理要完成第i个项目,状态k是否可行。然后类似于背包
给定状态s,去除子状态k后,就能完成第i项目了,所以dp[i][s] = max(dp[i][s], dp[i - ][s - k] + 1);
dp[i - 1][s - k] + 1表示用状态s - k去完成前i - 1个项目,能完成多少。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <string>
#include <stack>
#include <map>
#include <set>
#include <bitset>
#include <cstdlib>
#include <ctime>
#define X first
#define Y second
#define clr(u,v); memset(u,v,sizeof(u));
#define inff() freopen("data","r",stdin);
#define out() freopen("ans","w",stdout);
#define Clear(Q); while (!Q.empty()) Q.pop();
#define pb push_back
using namespace std;
typedef long long ll;
typedef long long LL;
typedef pair <int, int> pii;
const LL INF = 1e17;
const int inf = 0x3f3f3f3f;
const int maxn = 1e2 + ;
vector<int> project[maxn], man[maxn];
vector<int> state[maxn];
int solve[][maxn], DFN;
int dp[][ + ];
void work() {
int n, m; // n_project, m_man
scanf("%d%d", &n, &m);
for (int i = ; i <= n; ++i) { // project
int c;
scanf("%d", &c);
project[i].clear();
while (c--) {
int val;
scanf("%d", &val);
project[i].push_back(val);
}
}
for (int i = ; i <= m; ++i) { // man
int c;
scanf("%d", &c);
man[i].clear();
while (c--) {
int val;
scanf("%d", &val);
man[i].push_back(val);
}
}
int en = ( << m) - ;
for (int i = ; i <= n; ++i) {
state[i].clear();
for (int j = ; j <= en; ++j) {
++DFN;
for (int k = ; k <= m; ++k) {
if (j & ( << (k - ))) {
for (int d = ; d < man[k].size(); ++d) {
solve[i][man[k][d]] = DFN;
}
}
}
bool flag = true;
for (int d = ; d < project[i].size(); ++d) {
if (solve[i][project[i][d]] != DFN) {
flag = false;
break;
}
}
if (flag) state[i].push_back(j);
}
}
// for (int i = 1; i <= n; ++i) {
// for (int j = 0; j < state[i].size(); ++j) {
// printf("%d ", state[i][j]);
// }
// printf("\n");
// }
memset(dp, false, sizeof dp);
for (int i = ; i <= n; ++i) {
for (int d = ; d <= en; ++d) {
dp[i][d] = dp[i - ][d]; // 不做这个项目
for (int k = ; k < state[i].size(); ++k) {
if ((d | state[i][k]) > d) continue;
int res = d ^ state[i][k];
dp[i][d] = max(dp[i][d], dp[i - ][res] + );
}
}
}
int ans = ;
for (int i = ; i <= en; ++i) ans = max(ans, dp[n][i]);
static int f = ;
printf("Case #%d: %d\n", ++f, ans);
} int main() {
#ifdef local
freopen("data.txt", "r", stdin);
#endif
int T;
scanf("%d", &T);
while(T--) {
work();
}
return ;
}
Engineer Assignment HDU - 6006 状压dp的更多相关文章
- HDU 6006 状压dp
Engineer Assignment Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- HDU 4778 状压DP
一看就是状压,由于是类似博弈的游戏.游戏里的两人都是绝对聪明,那么先手的选择是能够确定最终局面的. 实际上是枚举最终局面情况,0代表是被Bob拿走的,1为Alice拿走的,当时Alice拿走且满足变换 ...
- HDU 3001 状压DP
有道状压题用了搜索被队友骂还能不能好好训练了,, hdu 3001 经典的状压dp 大概题意..有n个城市 m个道路 成了一个有向图.n<=10: 然后这个人想去旅行.有个超人开始可以把他扔到 ...
- hdu 2809(状压dp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2809 思路:简单的状压dp,看代码会更明白. #include<iostream> #in ...
- hdu 2167(状压dp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2167 思路:经典的状压dp题,前后,上下,对角8个位置不能取,状态压缩枚举即可所有情况,递推关系是为d ...
- hdu 3254 (状压DP) Corn Fields
poj 3254 n乘m的矩阵,1表示这块区域可以放牛,0,表示不能,而且不能在相邻的(包括上下相邻)两个区域放牛,问有多少种放牛的方法,全部不放也是一种方法. 对于每块可以放牛的区域,有放或者不放两 ...
- HDU 5823 (状压dp)
Problem color II 题目大意 定义一个无向图的价值为给每个节点染色使得每条边连接的两个节点颜色不同的最少颜色数. 对于给定的一张由n个点组成的无向图,求该图的2^n-1张非空子图的价值. ...
- hdu 4739 状压DP
这里有状态压缩DP的好博文 题目:题目比较神,自己看题目吧 分析: 大概有两种思路: 1.dfs,判断正方形的话可以通过枚举对角线,大概每次减少4个三角形,加上一些小剪枝的话可以过. 2.状压DP,先 ...
- Travel(HDU 4284状压dp)
题意:给n个城市m条路的网图,pp在城市1有一定的钱,想游览这n个城市(包括1),到达一个城市要一定的花费,可以在城市工作赚钱,但前提有工作证(得到有一定的花费),没工作证不能在该城市工作,但可以走, ...
随机推荐
- HUST高级软件工程--测试管理工具实践--Day3
测试管理工具实践--Day3 今天完成任务情况: 小靳 今天,大家参加考试,时间比较紧促.庆幸,自己的队伍比较给力,大家都没有拖后腿,深夜还在为自己的任务拼搏,很是激励人心 我今天的工作就是 学会了注 ...
- C#找到目录和其子目录的某个文件
string url = ""; string[] urls = Directory.GetFiles(目录, 文件名, SearchOption.AllDirectories); ...
- MVC身份验证.MVC过滤器.MVC6关键字Task,Async.前端模拟表单验证,提交.自定义匿名集合.Edge导出到Excel.BootstrapTree树状菜单的全选和反选.bootstrap可搜索可多选可全选下拉框
1.MVC身份验证. 有两种方式.一个是传统的所有控制器继承自定义Control,然后再里面用MVC的过滤器拦截.所以每次网站的后台被访问时.就会先走入拦截器.进行前端和后端的验证 一个是利用(MVC ...
- vue使用过程常见的一些问题
Vue目前的的开发模式主要有两种:1.直接页面级的开发,script直接引入Vue2.工程性开发,webpack+loader或者直接使用脚手架工具Vue-cli,里面的文件都配置好了 webpack ...
- 高效 MacBook 工作环境配置
转自:https://mp.weixin.qq.com/s/sloc6HgKcosXtWcbMB_5hA 工欲善其事,必先利其器,工具永远都是用来解决问题的,没必要为了工具而工具,一切工具都是为了能快 ...
- Loadrunner 性能测试笔记
性能测试脚本 // 关联token 放在请求返回前 web_reg_save_param("tokenId", "LB=,\"tokenId\":\& ...
- [51nod1190]最小公倍数之和V2(莫比乌斯反演)
题解 传送门 题解 我是真的不明白这玩意儿是怎么跟反演扯上关系的-- 首先 \[ \begin{align} ans &=b\sum_{d|b}{1\over d}\sum_{i=a}^{b} ...
- JavaScript学习笔记——1.了解JavaScript
百度百科: JavaScript是一种属于网络的脚本语言,已经被广泛用于Web应用开发,常用来为网页添加各式各样的动态功能,为用户提供更流畅美观的浏览效果.通常JavaScript脚本是通过嵌入在HT ...
- QueryTask,FindTask,IdentifyTask三种查询的区别
1:QueryTask是一个进行空间和属性查询的功能类,它可以在某个地图服务的某个子图层内进行查询,顺便需要提一下的是,QueryTask进行查询的地图服务并不必项加载到Map中进行显示.QueryT ...
- thrift 通信的使用 /安装
参考: http://blog.csdn.net/yohunl/article/details/41748511 http://blog.csdn.net/amuseme_lu/article/det ...