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 ...
随机推荐
- js中的控制结构for-in语句
var arr=['赵','钱','孙','李']; for(var i=0;i<arr.length;i++){ console.log(arr[i]); } var obj={ name:' ...
- 【总结整理】javascript基础入门学习(慕课网学习)
https://www.imooc.com/learn/36 注意: javascript作为一种脚本语言可以放在html页面中任何位置,但是浏览器解释html时是按先后顺序的,所以前面的script ...
- 开发工具 idea 激活方法
1. 到网站 http://idea.lanyus.com/ 获取注册码. 2.填入下面的license server: http://intellij.mandroid.cn/ http://ide ...
- schedule与scheduleAtFixedRate比较
schedule与scheduleAtFixedRate: 不延时: schedule(TimerTask, Date runDate, long period)方法任务不延时----Date类型 i ...
- mac上virtualBox的安装和使用
一.下载和安装 去oracle官网下载mac版的virtualBox. 官网下载地址:https://www.virtualbox.org/. 下载好后按照向导进行安装即可. 二.使用方法 1.新建虚 ...
- Java-马士兵设计模式学习笔记-迭代器模式-模仿Collectin ArrayList LinckedList
Java Iterator模式 Java Iterator模式, 模仿Collectin ArrayList LinckedList 一.有如下几个类 1.接口Collection.java 2.接口 ...
- p4213 【模板】杜教筛(Sum)
传送门 分析 我们知道 $\varphi * 1 = id$ $\mu * 1 = e$ 杜教筛即可 代码 #include<iostream> #include<cstdio> ...
- loj10100 网络
这个题目描述好难理解呀qwq... 传送门 分析 在读懂题之后我们不难发现这道题实际就是在求一张图中有多少个割点.只需要注意读入方式即可. 代码 #include<iostream> #i ...
- ARC100D Equal Cut
传送门 分析 首先我们想到的肯定是n^3暴力枚举,但这显然不行.然后我们想到的就是二分了,但这题没有什么单调性,所以二分也不行.这时候我就想到了先枚举找出p2的位置再在它的左右两边找到p1和p3,但是 ...
- Java Iterable类
查看java源代码 /* * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved. * ORACLE ...