首页 > 代码库 > 状态压缩DP

状态压缩DP

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;

int n,sum[20][1<<15],dp[20][1<<15],a[20][20],sta[1<<15];

int main()
{
    while(~scanf("%d",&a[1][1]))
    {
        n = 1;
        char c;
        while(~scanf("%d%c",&a[1][++n],&c) && c != \n);
        for(int i = 2;i <= n;i++)
        {
            for(int j = 1;j <= n;j++)   scanf("%d",&a[i][j]);
        }
        memset(dp,0,sizeof(dp));
        memset(sum,0,sizeof(sum));
        int cnt = 0,endd = 1<<n;
        for(int i = 0;i < endd;i++)
        {
            if(i & (i << 1))   continue;
            sta[++cnt] = i;
        }
        for(int i = 1;i <= n;i++)
        {
            for(int j = 1;j <= cnt;j++)
            {
                for(int k = 1;k <= n;k++)
                {
                    if(sta[j] & (1<<(k-1)))  sum[i][j] += a[i][k];
                }
            }
        }
        for(int i = 1;i <= cnt;i++)   dp[1][i] = sum[1][i];
        for(int i = 1;i < n;i++)
        {
            for(int j = 1;j <= cnt;j++)
            {
                for(int k = 1;k <= cnt;k++)
                {
                    if(sta[j] & sta[k])    continue;
                    if(sta[j] & (sta[k]<<1))    continue;
                    if(sta[j] & (sta[k]>>1))    continue;
                    dp[i+1][k] = max(dp[i+1][k],dp[i][j]+sum[i+1][k]);
                }
            }
        }
        int ans = 0;
        for(int i = 1;i <= cnt;i++) ans = max(ans,dp[n][i]);
        printf("%d\n",ans);
    }
    return 0;
}

//HDU2167 

 

状态压缩DP