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. GridLayout with span

    Widgets can span multiple columns or rows in a grid. In the next example we illustrate this. #!/usr/ ...

  2. QtGui.QSlider

    A QtGui.QSlider is a widget that has a simple handle. This handle can be pulled back and forth. This ...

  3. qs.js库 使用方法

    1.qs.js库说明 qs是一个url参数转化(parse和stringify)的js库. https://www.npmjs.com/package/qs 2.使用(以vue文件做示例) (1)基本 ...

  4. CallableStatement简单使用

    直接上存储过程.函数 --运行不带參数但带返回值的存储过程 CREATE OR REPLACE PROCEDURE proc_getUserCount(v_totalCount OUT NUMBER) ...

  5. php实现二维数组排序array_multisort($ages, SORT_DESC, $home)函数

    1.sql查询排序参数是order by,那么php进行排序呢 可以参考array_multisrot函数 //php进行二维数组排序 -xzz1009 foreach($home as $home) ...

  6. Cookie技术

    u  常用的API 创建Cookie对象 Cookie(String name, String value)    ->以指定数据创建Cookie对象 设置Cookie对象 void setMa ...

  7. LaTex 常见错误及解决办法

    出现错误: Multirow 要用库的  导入\usepackage{multirow} ,,即可

  8. 获取含有class为某个值的a标签或img标签

    <a\s+[^>]*class='fjLink'[^>]*>[^<]*</a>|<img\s+[^>]*class='fjLink'[^>] ...

  9. 使用spring AOP获得session的思路

    由于Spring 的AOP面向切面编程,与Servlet容器没有任何关联,所以想要获得Session会话比较麻烦. 当然Struts2同样不依赖Servlet容器,可以在Spring AOP中可以使用 ...

  10. bcdedit

    我的电脑装了双系统:Win2003 SP2(C盘)和Win2008 SP2(D盘),最近2003一启动就蓝屏unknown hard error,安全模式也进不去,恢复注册表等方法试过也不行,但200 ...