Petya and Spiders DLX+重复覆盖 最小支配集(最少多少个点能覆盖所有的点)

Little Petya loves training spiders. Petya has a board n × m in size. Each cell of the board initially has a spider sitting on it. After one second Petya chooses a certain action for each spider, and all of them humbly perform its commands. There are 5 possible commands: to stay idle or to move from current cell to some of the four side-neighboring cells (that is, one command for each of the four possible directions). Petya gives the commands so that no spider leaves the field. It is allowed for spiders to pass through each other when they crawl towards each other in opposite directions. All spiders crawl simultaneously and several spiders may end up in one cell. Petya wants to know the maximum possible number of spider-free cells after one second.

Input

The first line contains two space-separated integers n and m (1 ≤ n, m ≤ 40, n·m ≤ 40) — the board sizes.

Output

In the first line print the maximum number of cells without spiders.

Sample test(s)
Input
1 1
Output
0
Input
2 3
Output
4
Note

In the first sample the only possible answer is:

s

In the second sample one of the possible solutions is:

rdl
rul

s denotes command "stay idle", l, r, d, u denote commands "crawl left", "crawl right", "crawl down", "crawl up", correspondingly.


//


#include<stdio.h>
#include<math.h>
#include<string.h>
#include<algorithm>
#include<iostream>
using namespace std;
#define eps 1e-8
#define N 185
#define V 36000
int n,m;//n行 m列
int L[V],R[V];
int D[V],U[V];
int C[V];
int S[N],H[N],mark[V],OK[N],tans[V];
int ak,size;//ak 最少多少行可以覆盖所有列(可重复)
void Link(int r,int c)
{
    S[c]++;C[size]=c;
    U[size]=U[c];D[U[c]]=size;
    D[size]=c;U[c]=size;
    if(H[r]==-1) H[r]=L[size]=R[size]=size;
    else
    {
        L[size]=L[H[r]];R[L[H[r]]]=size;
        R[size]=H[r];L[H[r]]=size;
    }
    mark[size]=r;
    size++;
}
void remove(int c)
{
    int i;
    for(i=D[c];i!=c;i=D[i])
        L[R[i]]=L[i],R[L[i]]=R[i];
}
void resume(int c)
{
    int i;
    for(i=U[c];i!=c;i=U[i])
        L[R[i]]=R[L[i]]=i;
}
int h()
{
    int i,j,k,count=0;
    bool visit[N];
    memset(visit,0,sizeof(visit));
    for(i=R[0];i;i=R[i])
    {
        if(visit[i]) continue;
        count++;
        visit[i]=1;
        for(j=D[i];j!=i;j=D[j])
        {
            for(k=R[j];k!=j;k=R[k])
                visit[C[k]]=1;
        }
    }
    return count;
}
void Dance(int k)
{
    int i,j,c,Min,ans;
    ans=h();
    if(k+ans>=ak) return;
    if(!R[0])
    {
        if(k<ak)
        {
            ak=k;
            for(int i=0;i<k;i++)
            {
                tans[i]=mark[OK[i]];
            }
        }
        return;
    }
    for(Min=N,i=R[0];i;i=R[i])
        if(S[i]<Min) Min=S[i],c=i;
    for(i=D[c];i!=c;i=D[i])
    {
        OK[k]=i;
        remove(i);
        for(j=R[i];j!=i;j=R[j])
            remove(j);
        Dance(k+1);
        for(j=L[i];j!=i;j=L[j])
            resume(j);
        resume(i);
    }
    return;
}
int xx[5]={0,0,0,1,-1};
int yy[5]={0,1,-1,0,0};
int mat[400][400];
int main()
{
    int tn,tm;
    while(scanf("%d%d",&tn,&tm)==2)
    {
        memset(mat,0,sizeof(mat));
        for(int i=1;i<=tn;i++)
        {
            for(int j=1;j<=tm;j++)
            {
                for(int k=0;k<5;k++)
                {
                    int x=i+xx[k],y=j+yy[k];
                    if(x>=1&&x<=tn&&y>=1&&y<=tm)
                    {
                        mat[(i-1)*tm+j][(x-1)*tm+y]=1;
                    }
                }
            }
        }
        n=tn*tm,m=tn*tm;
        //DLX
        for(int i=0;i<=m;i++)
        {
            S[i]=0;
            U[i]=D[i]=i;
            L[i+1]=i;R[i]=i+1;
        }R[m]=0;
        memset(H,-1,sizeof(H));
        memset(mark,0,sizeof(mark));
        memset(tans,0,sizeof(tans));
        size=m+1;
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {//cout<<i<<".."<<j<<endl;
                if(mat[i][j]) Link(i,j);
            }
        }
        ak=N;
        Dance(0);
        printf("%d\n",n-ak);
       // for(int i=0;i<ak;i++) cout<<tans[i]<<endl;
    }
    return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值