POJ 2441 Arrange The Bulls (state compression dp+ scrolling array)

Source: Internet
Author: User

Test instructions

n Cattle and M-barn, each cow has its own favorite P-Barn (1<=p<=m), asking how many ways to arrange for the N-cow.

Analysis:

DP[I][J] Represents the number of methods of the I cow in J state.

DP[I][J] = Sigma (Dp[i-1][k]) (k from 0 to 1<<m).

To be optimized with a scrolling array, otherwise dp[20][1<<20] will mle!

Note the emptying of the scrolling array = =!

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>Ten#include <Set> One  A using namespacestd; -  -typedefLong Longll; the  -vector<int> p[ -]; - intdp[2][1<< -],n; -  + intMain () - { +     intm,x,y; A      while(~SCANF ("%d%d",&n,&m)) at     { -          for(intI=1; i<=n;i++) -         { - p[i].clear (); -scanf"%d",&x); -              while(x--) in             { -scanf"%d",&y); to p[i].push_back (y); +             } -         } theMemset (DP,0,sizeof(DP)); *  $         intAll = (1&LT;&LT;M)-1;Panax Notoginsengdp[0][0] =1; -         intT; the  +          for(intI=1; i<=n;i++) A         { the              for(intk=0; k<=all;k++)//every state access once +             { -                 if(!dp[(i1) &1][K])Continue;//It is only when the K-state of the former I-1 cow has a value that it is possible to determine the method of the first cow $                  for(intj=0; J<p[i].size (); j + +) $                 { -t = p[i][j]-1; -                     if((k>>t) &1)Continue;//If there is a cow in the K state, it cannot be placed . thedp[i&1][k| (1&LT;&LT;T)] + = dp[(i-1) &1][k]; -                 }Wuyi             } thememset (dp[(i-1) &1],0,sizeof(Dp[(i1) &1]));//clear the next dimension to use for the scroll array -         } Wu  -         intAns =0; About          for(intI=1; i<=all;i++) $         { -ans+=dp[n&1][i]; -         } -printf"%d\n", ans); A     } +     return 0; the}
View Code

POJ 2441 Arrange The Bulls (state compression dp+ scrolling array)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.