您的位置:首页 > 其它

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

2011-10-11 21:47 465 查看
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
,H
,mark[V],OK
,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
;

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;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐