poj 2441 Arrange the Bulls (状态压缩dp+滚动数组)

题意:

n头牛和m个barn,每头牛有自己喜欢的p个barn(1<=p<=m),问有多少种安排n头牛的方法。

分析:

dp[i][j]表示i头牛在j状态下的方法数。

dp[i][j] = sigma(dp[i-1][k])  (k从0到 1<<m)。

要用滚动数组优化,否则dp[20][1<<20]会MLE!

注意滚动数组的清空= =!

 1 #include<stdio.h>
 2 #include<cstring>
 3 #include<iostream>
 4 #include<algorithm>
 5 #include<cstdlib>
 6 #include<cmath>
 7 #include<vector>
 8 #include<queue>
 9 #include<map>
10 #include<set>
11
12 using namespace std;
13
14 typedef long long ll;
15
16 vector<int> p[25];
17 int dp[2][1<<20],n;
18
19 int main()
20 {
21     int m,x,y;
22     while(~scanf("%d%d",&n,&m))
23     {
24         for(int i=1;i<=n;i++)
25         {
26             p[i].clear();
27             scanf("%d",&x);
28             while(x--)
29             {
30                 scanf("%d",&y);
31                 p[i].push_back(y);
32             }
33         }
34         memset(dp,0,sizeof(dp));
35
36         int all = (1<<m)-1;
37         dp[0][0] = 1;
38         int t;
39
40         for(int i=1;i<=n;i++)
41         {
42             for(int k=0;k<=all;k++)  //每种状态访问一次
43             {
44                 if(!dp[(i-1)&1][k]) continue; //只有前i-1头牛的k状态有值的情况下才能确定第i头牛的放法
45                 for(int j=0;j<p[i].size();j++)
46                 {
47                     t = p[i][j]-1;
48                     if((k>>t)&1) continue; //如果k状态下这个位置已经有牛则不能放
49                     dp[i&1][k|(1<<t)] += dp[(i-1)&1][k];
50                 }
51             }
52             memset(dp[(i-1)&1],0,sizeof(dp[(i-1)&1]));  //清空滚动数组下次要用的一维
53         }
54
55         int ans = 0;
56         for(int i=1;i<=all;i++)
57         {
58             ans+=dp[n&1][i];
59         }
60         printf("%d\n",ans);
61     }
62     return 0;
63 }

时间: 2024-10-13 15:42:16

poj 2441 Arrange the Bulls (状态压缩dp+滚动数组)的相关文章

POJ 2441 Arrange the Bulls(状态压缩DP)

Arrange the Bulls Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 4234   Accepted: 1612 Description Farmer Johnson's Bulls love playing basketball very much. But none of them would like to play basketball with the other bulls because the

poj 2441 Arrange the Bulls 状压dp入门

题意: 将n头牛和m个栏做匹配,求匹配方案数. 分析: 开始暴搜tle了,还是要用状压dp,dp[i][s]表示前i头牛匹配栏的状态为s时可行的方案数. 代码: //poj 2441 //sep9 #include <iostream> using namespace std; const int maxN=21; int dp[2][1<<maxN]; int a[maxN][maxN]; int main() { int n,m; scanf("%d%d",

poj 3254 Corn Fields ,状态压缩DP

题目链接 题意: 一个矩阵里有很多格子,每个格子有两种状态,可以放牧和不可以放牧,可以放牧用1表示,否则用0表示,在这块牧场放牛,要求两个相邻的方格不能同时放牛,即牛与牛不能相邻.问有多少种放牛方案(一头牛都不放也是一种方案) state[i] 表示对于一行,保证不相邻的方案 状态:dp[i][ state[j] ]  在状态为state[j]时,到第i行符合条件的可以放牛的方案数 状态转移:dp[i][ state[j] ] =Sigma dp[i-1][state'] (state'为符合条

POJ 1038 Bugs Integrated, Inc. 状态压缩DP

题目来源:1038 Bugs Integrated, Inc. 题意:最多能放多少个2*3的矩形 思路:状态压缩DP啊 初学 看着大牛的代码搞下来的  总算搞懂了 接下来会更轻松吧 3进制代表前2行的状态(i行和i-1行)1代表i-1行占位 2代表i行占位 i-1不管有没有占位都不会影响的0代表i行和i-1行都空闲 然后枚举状态dfs更新状态 话说就不能没写深搜了 有点不会了 #include <cstdio> #include <cstring> #include <alg

[POJ 2411] Mondriaan&#39;s Dream 状态压缩DP

题意 给定一个 n * m 的矩形. 问有多少种多米诺骨牌覆盖. n, m <= 11 . 实现 1 #include <cstdio> 2 #include <cstring> 3 #include <cstdlib> 4 #include <cctype> 5 #define F(i, a, b) for (register int i = (a); i <= (b); i++) 6 #define LL long long 7 inline

poj 3254 Corn Fields(状态压缩dp)

Description Farmer John has purchased a lush new rectangular pasture composed of M by N (1 ≤ M ≤ 12; 1 ≤ N ≤ 12) square parcels. He wants to grow some yummy corn for the cows on a number of squares. Regrettably, some of the squares are infertile and

POJ 2288 Islands And Bridges 状态压缩dp+哈密顿回路

题意:n个点 m条边的图,路径价值定义为相邻点乘积,若路路径c[i-1]c[i]c[i+1]中c[i-1]-c[i+1]有边 则价值加上三点乘积找到价值最大的哈密顿回路,和相应的方法数n<=13.暴力dfs O(13!) TLE 由于n<13 经典的状态压缩dp [状态] [当前点位置 ][前一点位置] 注意上一个状态必须合法才能进行转移设状态dp[s][i][j] 当前状态为s,当前点为i上一个点为j的最大价值, ans=max(dp[(1<<n)-1][i])dp[s][i][

poj 2441 Arrange the Bulls(状态压缩dp)

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

poj 2441 Arrange the Bulls(状压DP入门)

Arrange the Bulls Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 3509   Accepted: 1344 Description Farmer Johnson's Bulls love playing basketball very much. But none of them would like to play basketball with the other bulls because the