zoukankan      html  css  js  c++  java
  • (状态压缩DP) poj 2441

    Arrange the Bulls
    Time Limit: 4000MS   Memory Limit: 65536K
    Total Submissions: 3709   Accepted: 1422

    Description

    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

    Source

     
     
    题意:
     
    有n头牛 m个帐篷,n头牛进入m个帐篷方案数
     
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<cstdlib>
    #include<algorithm>
    #include<string>
    using namespace std;
    int n,m,mp[21][21];
    int dp[(1<<20)+2];
    int main()
    {
        scanf("%d%d",&n,&m);
        for(int i=0;i<n;i++)
        {
            int x,y;
            scanf("%d",&x);
            while(x--)
            {
                scanf("%d",&y);
                y--;
                mp[i+1][y]=1;
            }
        }
        if(n>m)
        {
            printf("0
    ");
            return 0;
        }
        dp[0]=1;
        for(int i=0;i<n;i++)
        {
            for(int j=(1<<m)-1;j>=0;j--)
            {
                if(dp[j]==0)
                    continue;
                for(int k=0;k<m;k++)
                {
                    if((j&(1<<k))!=0)
                        continue;
                    if(mp[i+1][k]==0)
                        continue;
                    dp[j|(1<<k)]+=dp[j];
                }
                dp[j]=0;
            }
        }
        int ans=0;
        for(int i=0;i<(1<<m);i++)
            ans+=dp[i];
        printf("%d
    ",ans);
        return 0;
    }
    

      

  • 相关阅读:
    【算法导论】第11章,散列表
    【算法导论】第10章,基本数据结构
    【推荐系统实践】冷启动问题
    【算法导论】第8、9章,线性时间排序,中位数顺序统计量
    【推荐系统实践】协同过滤
    Java编程思想---第五章 初始化与清理(下)
    Java编程思想---第五章 初始化与清理(上)
    Java编程思想---第四章 控制执行流程
    Java编程思想---第三章 操作符
    去除MyEclipse频繁弹出的Update Progress窗口
  • 原文地址:https://www.cnblogs.com/water-full/p/4488723.html
Copyright © 2011-2022 走看看