Arrange the Bulls POJ - 2441 (状压dp)

Farmer Johnson’s Bulls love playing basketball very much. But none of them would like to play basketball with the other bulls because they believe that the others are all very weak. Farmer Johnson has N cows (we number the cows from 1 to N) and M barns (we number the barns from 1 to M), which is his bulls’ basketball fields. However, his bulls are all very captious, they only like to play in some specific barns, and don’t want to share a barn with the others.

So it is difficult for Farmer Johnson to arrange his bulls, he wants you to help him. Of course, find one solution is easy, but your task is to find how many solutions there are.

You should know that a solution is a situation that every bull can play basketball in a barn he likes and no two bulls share a barn.

To make the problem a little easy, it is assumed that the number of solutions will not exceed 10000000.
Input
In the first line of input contains two integers N and M (1 <= N <= 20, 1 <= M <= 20). Then come N lines. The i-th line first contains an integer P (1 <= P <= M) referring to the number of barns cow i likes to play in. Then follow P integers, which give the number of there P barns.
Output
Print a single integer in a line, which is the number of solutions.
Sample Input
3 4
2 1 4
2 1 3
2 2 4
Sample Output
4

大致题意:有n头牛编号1到n,m个场地编号1到m,每头牛都有几个自己喜欢的场地,每个场地只能放置一头牛,问满足这n头牛的需求的总的情况数。

思路:用二进制来压缩这m个场地分配的状态,0表示没占用,1表示占用,一共有(1<<20)种状态。
假设dp[i][j]为第i头牛在j状态下的分配方式总数,dp[i][j]+=dp[i-1][k](0<=k<(1<<20),j能由k状态转移过来)。所以当前牛的分配总数只与上一头牛相关,所以可以用滚动数组来优化内存,否则会爆掉。

代码如下

#include <iostream> 
#include <cmath>
#include <algorithm>
#include <cstring>
#include <queue>
#include <cstdio>
#include <map>
using namespace std; 
#define ll long long int 
const int INF=0x3f3f3f3f;  
int dp[2][1<<20];
int mp[25][25];//mp[i][j]=1表示第i号牛喜欢j号场地,=0是不喜欢
int main()  
{    
    int n,m;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        memset(mp,0,sizeof(mp));
        memset(dp,0,sizeof(dp));

        for(int i=1;i<=n;i++)
        {
            int t;
            scanf("%d",&t);
            while(t--)
            {
                int j;
                scanf("%d",&j);
                mp[i][j-1]=1;//场地编号从0开始记,方便状态表示
            }
        }
        dp[0][0]=1;
        for(int i=1;i<=n;i++)
        {
            for(int j=0;j<(1<<m);j++)
            {
                if(dp[1-(i&1)][j]==0)//剪枝,如果上一头牛不能转移到合法状态j
                continue;
                for(int k=0;k<m;k++)//假设把当前第i号牛放到k号场地。
                {
                    if(mp[i][k]&&(j!=(j|(1<<k))))//如果i号牛喜欢k号场地,且k号场地没被占用
                    dp[i&1][j|(1<<k)]+=dp[1-(i&1)][j];

                }

            }
            memset(dp[1-(i&1)],0,sizeof(dp[1-(i&1)]));
        }
        int sum=0;
        for(int i=1;i<(1<<m);i++)
            sum+=dp[n&1][i];
        printf("%d\n",sum);
     } 

    return 0;
}    
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值