http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1758

晕。。。。状压没考虑循环方向然后错了好久。。

这点要注意。。。(其实就是01背包变成了完全背包QAQ

我们将课程拆成两个点,然后状压

那么答案就是(1<<(s<<1))-1

转移就不说了,,,,,太简单。。

#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
using namespace std;
typedef long long ll;
#define pii pair<int, int>
#define pii pair<int, int>
#define mkpii make_pair<int, int>
#define pdi pair<double, int>
#define mkpdi make_pair<double, int>
#define pli pair<ll, int>
#define mkpli make_pair<ll, int>
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << (#x) << " = " << (x) << endl
#define error(x) (!(x)?puts("error"):0)
#define printarr2(a, b, c) for1(_, 1, b) { for1(__, 1, c) cout << a[_][__]; cout << endl; }
#define printarr1(a, b) for1(_, 1, b) cout << a[_] << '\t'; cout << endl
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }
inline const int max(const int &a, const int &b) { return a>b?a:b; }
inline const int min(const int &a, const int &b) { return a<b?a:b; } const int N=17, oo=0x3f3f3f3f;
int f[1<<N], n, m, s, sum[N];
struct dat { int c, s; }a[105], b[105]; void readin(dat arr[], int n) {
for1(i, 1, n) {
arr[i].s=0; arr[i].c=oo;
read(arr[i].c); //dbg(arr[i].c);
char ch=getchar();
int r=0;
while(ch!='\n' && ch!='\r') {
if(ch<'0'||ch>'9') {
if(r!=0) arr[i].s|=(3<<((r-1)<<1));
r=0;
while(ch<'0'||ch>'9') {
ch=getchar();
if(ch=='\n' || ch=='\r') break;
}
if(ch=='\n' || ch=='\r') break;
}
r=r*10+ch-'0';
ch=getchar();
}
if(r!=0) arr[i].s|=(3<<((r-1)<<1));
//rep(k, s) if(arr[i].s&(3<<(k<<1))) printf("%d ", k+1);
//puts("");
}
}
void Prin(int x) {
for3(k, ((s-1)<<1)+1, 0) { printf("%d", (bool)(x&(1<<k))); if((k&1)==0) printf(" "); }
puts("");
}
int main() {
read(s); read(n); read(m);
while(s) {
int all=(1<<(s<<1))-1;
CC(f, 0x3f); CC(sum, 0);
readin(a, n); readin(b, m); int tc=0, tn=0;
for1(i, 1, n) rep(k, s) if((a[i].s>>(k<<1))&3) sum[k]++;
rep(k, s) if(sum[k]) {
int t=1;
if(sum[k]>=2) t=3;
tc|=(t<<(k<<1));
}
for1(i, 1, n) tn+=a[i].c;
f[tc]=tn; for1(i, 1, m) {
//dbg(i);
for3(j, all, 0) if(f[j]!=oo) {
int nx=0; //Prin(j);
rep(k, s) if(b[i].s&(3<<(k<<1))) {
int now=(j>>(k<<1))&3;
if(now==0 || now==2) nx|=1<<(k<<1);
if(now==1 || now==3) nx|=2<<(k<<1);
}
f[j|nx]=min(f[j|nx], f[j]+b[i].c); //Prin(nx); Prin(j|nx); puts("");
} }
if(f[all]==oo) f[all]=-1;
printf("%d\n", f[all]);
read(s); read(n); read(m);
}
return 0;
}

  

【UVa】Headmaster's Headache(状压dp)的更多相关文章

  1. UVa 10817 Headmaster's Headache (状压DP+记忆化搜索)

    题意:一共有s(s ≤ 8)门课程,有m个在职教师,n个求职教师.每个教师有各自的工资要求,还有他能教授的课程,可以是一门或者多门. 要求在职教师不能辞退,问如何录用应聘者,才能使得每门课只少有两个老 ...

  2. UVa 1204 Fun Game (状压DP)

    题意:有一些小孩(至少两个)围成一圈,有 n 轮游戏,每一轮从某个小孩开始往左或者往右伟手帕,拿到手帕写上自己的性别(B,G),然后以后相同方向给下一个. 然后在某个小孩结束,给出 n 轮手帕上的序列 ...

  3. UVa 11825 Hackers' Crackdown (状压DP)

    题意:给定 n 个计算机的一个关系图,你可以停止每台计算机的一项服务,并且和该计算机相邻的计算机也会终止,问你最多能终止多少服务. 析:这个题意思就是说把 n 台计算机尽可能多的分成一些组,使得每组的 ...

  4. UVA - 10817 Headmaster's Headache (状压类背包dp+三进制编码)

    题目链接 题目大意:有S门课程,N名在职教师和M名求职者,每名在职教师或求职者都有自己能教的课程集合以及工资,要求花费尽量少的钱选择一些人,使得每门课程都有至少两人教.在职教师必须选. 可以把“每个课 ...

  5. UVa 1252 Twenty Questions (状压DP+记忆化搜索)

    题意:有n件物品,每件物品有m个特征,可以对特征进行询问,询问的结果是得知某个物体是否含有该特征,要把所有的物品区分出来(n个物品的特征都互不相同), 最小需要多少次询问? 析:我们假设心中想的那个物 ...

  6. UVA - 1252 Twenty Questions (状压dp+vis数组加速)

    有n个物品,每个物品有m个特征.随机选择一个物品让你去猜,你每次可以询问一个特征的答案,问在采取最优策略时,最坏情况下需要猜的次数是多少. 设siz[S]为满足特征性质集合S的特征的物品总数,dp[S ...

  7. UVA 11825 Hackers’ Crackdown 状压DP枚举子集势

    Hackers’ Crackdown Miracle Corporations has a number of system services running in a distributed com ...

  8. 状压DP UVA 10817 Headmaster's Headache

    题目传送门 /* 题意:学校有在任的老师和应聘的老师,选择一些应聘老师,使得每门科目至少两个老师教,问最少花费多少 状压DP:一看到数据那么小,肯定是状压了.这个状态不好想,dp[s1][s2]表示s ...

  9. UVA 1412 Fund Management (预处理+状压dp)

    状压dp,每个状态可以表示为一个n元组,且上限为8,可以用一个九进制来表示状态.但是这样做用数组开不下,用map离散会T. 而实际上很多九进制数很多都是用不上的.因此类似uva 1601 Mornin ...

  10. UVa 11825 (状压DP) Hackers' Crackdown

    这是我做状压DP的第一道题,状压里面都是用位运算来完成的,只要耐下心来弄明白每次位运算的含义,还是容易理解的. 题意: 有编号为0~n-1的n台服务器,每台都运行着n中服务,每台服务器还和若干台其他服 ...

随机推荐

  1. Map 和 WeakMap 数据结构

    Map 和 WeakMap 是ES6 新增的数据结构 一.Map 它们本质与对象一样,都是键值对的集合,但是他们与 Object 对象主要的不同是,键可以是各种类型的数值,而Object 对象的键 只 ...

  2. leetCode(29):Happy Number

    Write an algorithm to determine if a number is "happy". A happy number is a number defined ...

  3. pdf+iphone+wechat

    可能很多人要问,为啥标题取这个名字. 因为今天在这个上面踩了太多坑.. 我们的需求其实很简单.做一个页面,把pdf文档嵌进去,在线显示. 如此需求,放在PC上chrome浏览器,一个embed标签就搞 ...

  4. ie6 javascript:void(0);

    遇到过几次这种问题,现在总结一下. 代码: <a onclick="window.location.href='http://www.google.com'" href=&q ...

  5. Linux防火墙的关闭和开启(转)

    1) 重启后生效 开启: chkconfig iptables on 关闭: chkconfig iptables off 2) 即时生效,重启后失效 开启: service iptables sta ...

  6. Android屏幕计量单位详解

    1.px (pixels)(像素):是屏幕的物理像素点,与密度相关,密度大了,单位面积上的px会比较多.通常不推荐使用这个. 2.dip或dp(与密度无关的像素):一个基于density的抽象单位,这 ...

  7. nginx配置文件和一些用法

    fastcgi配置: location ~ \.php$ { #fastcgi_pass unix:///tmp/php-cgi.sock; fastcgi_pass unix__tmp_php5_c ...

  8. Spark SQL and DataFrame Guide(1.4.1)——之DataFrames

    Spark SQL是处理结构化数据的Spark模块.它提供了DataFrames这样的编程抽象.同一时候也能够作为分布式SQL查询引擎使用. DataFrames DataFrame是一个带有列名的分 ...

  9. centos 7 搭建git远程仓储 免密登录

    第一步.安装git服务 yum install git 第二步.创建git用户 adduser git 第三步开启公钥验证 vi /etc/ssh/sshd_config 讲文件中的 #PubkeyA ...

  10. 常用音频软件:Wavesufer

    作者:桂. 时间:2017-06-02  10:23:39 链接:http://www.cnblogs.com/xingshansi/p/6932408.html 前言 只列举两个自己用过的(wave ...