HDU 4778 gems fight! (Status compression DP)

Source: Internet
Author: User

It is also a State compression. At the beginning, I always thought about the method of the game, and I always had no idea.

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int N=21;const int inf=0x3f3f3f3f;int g,n,s;int sum[1<<N];int res[10];int c[22][10];int dp[1<<N];int main(){//    freopen("in","r",stdin);    while(scanf("%d%d%d",&g,&n,&s),g|n|s){        memset(c,0,sizeof c);        for(int i=0;i<n;i++){            int k,x;            scanf("%d",&k);            while(k--){                scanf("%d",&x);                c[i][x-1]++;            }        }        int all=1<<n;        for(int i=0;i<all;i++){            sum[i]=0;            memset(res,0,sizeof res);            for(int j=0;j<n;j++){                if(i&(1<<j)){                    for(int k=0;k<g;k++)res[k]+=c[j][k];                }            }            for(int j=0;j<g;j++)sum[i]+=res[j]/s;        }        dp[0]=0;        for(int i=1;i<all;i++){            dp[i]=-inf;            for(int j=0;j<n;j++){                if(i&(1<<j)){                    int pre=i^(1<<j);                    int now=(all-1)^pre;                    int tmp=sum[now]-sum[now^(1<<j)];                    if(tmp)dp[i]=max(dp[i],dp[pre]+tmp);                    else dp[i]=max(dp[i],-dp[pre]);                }            }        }        printf("%d\n",dp[all-1]);    }    return 0;}

 

HDU 4778 gems fight! (Status compression DP)

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.