POJ 2441 Arrange the Bulls(状态压缩DP)
题意很简单,n头牛,m个位置,每头牛有各自喜欢的位置,问安排这n头牛使得每头牛都在各自喜欢的位置有几种安排方法。
2000MS代码:
#include <cstdio>
#include <cstring>
int dp[(<<)+];
int one[( << ) + ];
//用来数出状态为i时1的个数,具体到这个题中就是
//状态为i时有多少头牛已经安排好牛棚
void CountOne(int m)
{
for(int i=; i< ( << m); ++i)
{
int num=;
for(int j=; j< m; ++j)
{
if( (i & ( << j)) != )//i的第j位置是否为一
++num;
}
one[i] = num;
}
}
int main()
{
// freopen("in.cpp","r",stdin);
int n,m;
scanf("%d%d",&n,&m);
CountOne(m);
memset(dp,,sizeof(dp));
dp[] = ; //一头牛都没有安排,状态为0的满足条件的方案数为1
for(int i=; i<=n; ++i)
{
int cnt; //每头牛喜欢住的牛棚数
scanf("%d",&cnt);
while(cnt--)
{
int k; //该牛棚编号
scanf("%d",&k);
--k;//使得牛棚编号为 0 ~ m-1
for(int j=; j< ( << m); ++j)
{
if((j & ( << k)) != && one[j] == i) //这个状态已经安排好了i头牛,且第k个牛棚安排的是第i头牛
dp[j] += dp[j-(<<k)];
}
}
}
// 最终结果为安排了n头牛的状态满足条件的方案数的总和
int ans=;
for(int j=; j< ( << m ); ++j)
{
if(one[j] == n)
{
ans += dp[j];
}
}
printf("%d\n",ans);
return ;
}
90MS
#include <stdio.h>
#include <string.h> int map[][];
int now[(<<)+]; int main()
{
int i,j,n,m,k,x,p,ret;
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(map,,sizeof(map));
for (i=;i<n;i++)
{
scanf("%d",&k);
while(k--)
{
scanf("%d",&x);
x--;//0~m-1编号
map[i+][x]=;
}
}
if (n>m)
{
printf("0\n");
continue;
}
memset(now,,sizeof(now));
now[]=;
for (i=;i<n;i++)
{
for (j=(<<m)-;j>=;j--)
{
if (now[j]==) continue;
for (k=;k<m;k++)
{
if ((j & (<<k))!=) continue;//判断j的第k的位置为1
if (map[i+][k]==) continue;//在k的棚没有位置
p=(j | (<<k));//j的第k的位置变1
now[p]+=now[j];
}
now[j]=;
}
}
ret=;
for (i=;i<(<<m);i++)
{
ret+=now[i];
}
printf("%d\n",ret);
}
return ;
}
POJ 2441 Arrange the Bulls(状态压缩DP)的更多相关文章
- POJ 2441 Arrange the Bulls 状态压缩递推简单题 (状态压缩DP)
推荐网址,下面是别人的解题报告: http://www.cnblogs.com/chasetheexcellence/archive/2012/04/16/poj2441.html 里面有状态压缩论文 ...
- POJ 2441 Arrange the Bulls 状压dp
题目链接: http://poj.org/problem?id=2441 Arrange the Bulls Time Limit: 4000MSMemory Limit: 65536K 问题描述 F ...
- POJ 1691 Painting a Board(状态压缩DP)
Description The CE digital company has built an Automatic Painting Machine (APM) to paint a flat boa ...
- poj 3311 floyd+dfs或状态压缩dp 两种方法
Hie with the Pie Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 6436 Accepted: 3470 ...
- POJ 2686_Traveling by Stagecoach【状态压缩DP】
题意: 一共有m个城市,城市之间有双向路连接,一个人有n张马车票,一张马车票只能走一条路,走一条路的时间为这条路的长度除以使用的马车票上规定的马车数,问这个人从a出发到b最少使用时间. 分析: 状态压 ...
- poj 2441 Arrange the Bulls(状态压缩dp)
Description Farmer Johnson's Bulls love playing basketball very much. But none of them would like to ...
- poj 2441 Arrange the Bulls
Arrange the Bulls Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 5427 Accepted: 2069 ...
- poj 2411 Mondriaan's Dream_状态压缩dp
题意:给我们1*2的骨牌,问我们一个n*m的棋盘有多少种放满的方案. 思路: 状态压缩不懂看,http://blog.csdn.net/neng18/article/details/18425765 ...
- poj 1185 炮兵阵地 [经典状态压缩DP]
题意:略. 思路:由于每个大炮射程为2,所以如果对每一行状态压缩的话,能对它造成影响的就是上面的两行. 这里用dp[row][state1][state2]表示第row行状态为state2,第row- ...
- POJ 1038 Bug Integrated Inc(状态压缩DP)
Description Bugs Integrated, Inc. is a major manufacturer of advanced memory chips. They are launchi ...
随机推荐
- LookupError: unknown encoding: cp65001解决办法
一.之前手上做的一个web项目,漏洞频发,服务器用的是菜鸟云服务器,那个应急响应中心不错,想不到乌云倒了,白帽子竟然被阿里系养了,题外话了,首先感谢白帽子提的漏洞,同时也感慨自己安全知识,以及意识的薄 ...
- python笔记--5--文件操作
文件内容操作三部曲:打开.读写.关闭 open(file, mode='r', buffering=1, encoding=None, errors=None, newline=None, close ...
- jquery获取元素在文档中的位置信息以及滚动条位置(转)
jquery获取元素在文档中的位置信息以及滚动条位置 http://blog.csdn.net/qq_34095777/article/details/78750886 原文链接 原创 201 ...
- 将字符串str1复制为字符串str2的三种方法
1.自己编写函数,将两个字符串进行复制 #include<iostream> using namespace std; int main(){ char str1[]="I lo ...
- IO流框架关系总结(关系图)
字节流和字符流关系图 打印流和序列化流关系图
- 解决eclipse的source not found change at.
eclise快捷键F3跳转到类的实现方法,出现如图所示问题:source not found change atttached source.点击下图红圈,Change Attached Sou ...
- postgre教程
http://www.yiibai.com/html/postgresql/2013/080890.html
- PHP 中for循环的一个小小改进
注意 : 1, $size这个值可以放在for循环中的第一个 ';' 前声明, 因为这个声明只会执行一次; 2, 第二个 ';' 中的内容, 会重复运行, 所以$i < $size 这个判断会每 ...
- 转【C#调用DLL的几种方法,包括C#调用C\C++\C#DLL】
C#中dll调用方法 一. DLL与应用程序 动态链接库(也称为DLL,即为“Dynamic Link Library”的缩写)是Microsoft Windows最重要的组成要素之一, ...
- SQLServer存储引擎——03.日志
3. SQLServer存储引擎之日志篇 (3.1)日志结构 (3.1.1)物理日志 (0)物理日志即数据库的.ldf文件, 当然后缀名是可以自定义的,默认是.ldf (1)一个SqlServer数据 ...