codevs1022

来源:互联网 发布:软件 设计说明书 编辑:程序博客网 时间:2024/06/07 10:12

有一个N×M的单位方格中,其中有些方格是水塘,其他方格是陆地。如果要用1×2的矩阵区覆盖(覆盖过程不容许有任何部分重叠)这个陆地,那么最多可以覆盖多少陆地面积。

 

codevs <wbr>1022


分析:一道很水的二分图匹配,染色染好后直接dinic求最大匹配数



代码:
 #include
#include
#include
#include
#include
using namespace std;
int map[101][101];
int n,g,m;
const
 int 
 INF=1<<30;
inthead[20002],next[50002],end,start=0,from[50002],to[50002],cap[50002],flow[52000];
bool is_start(int x,int y)
 {
  return 0==(x-y)%2;//0 1
 }
int sum,side=1;

void read()
{
string s1;
scanf("%d%d",&n,&m);
scanf("%d",&g);
     inti,j,k,l;
for(i=1;i<=g;i++)
   {
     scanf("%d%d",&j,&k);
  map[j][k]=-1;
     }
  for(i=1;i<=n;i++)
    for(j=1;j<=m;j++)
      if(map[i][j]!=-1)
                  map[i][j]=++sum;
   end=++sum;  
  for(i=1;i<=n;i++)
    for(j=1;j<=m;j++)
  {
    if(map[i][j]!=-1)
    {
    if(is_start(i,j))
     {
     next[++side]=head[0];
     head[0]=side;
     to[side]=map[i][j];
     from[side]=0;
     cap[side]=1;
     
     next[++side]=head[map[i][j]];
     head[map[i][j]]=side;
     to[side]=0;
     from[side]=map[i][j];
     cap[side]=0;
     
     if(i+1<=n&&map[i+1][j]!=-1)
       {
       next[++side]=head[map[i][j]];
        head[map[i][j]]=side;
        to[side]=map[i+1][j];
        from[side]=map[i][j];
        cap[side]=1;
       next[++side]=head[map[i+1][j]];
        head[map[i+1][j]]=side;
        to[side]=map[i][j];
        from[side]=map[i+1][j];
        cap[side]=0;
       }
     if(i-1>=1&&map[i-1][j]!=-1)
       {
       next[++side]=head[map[i][j]];
        head[map[i][j]]=side;
        to[side]=map[i-1][j];
        from[side]=map[i][j];
        cap[side]=1;
       next[++side]=head[map[i-1][j]];
        head[map[i-1][j]]=side;
        to[side]=map[i][j];
        from[side]=map[i-1][j];
        cap[side]=0;
       }
       if(j+1<=m&&map[i][j+1]!=-1)
       {
       next[++side]=head[map[i][j]];
        head[map[i][j]]=side;
        to[side]=map[i][j+1];
        from[side]=map[i][j];
        cap[side]=1;
       next[++side]=head[map[i][j+1]];
        head[map[i][j+1]]=side;
        to[side]=map[i][j];
        from[side]=map[i][j+1];
        cap[side]=0;
       }
         if(j-1>=1&&map[i][j-1]!=-1)
       {
       next[++side]=head[map[i][j]];
        head[map[i][j]]=side;
        to[side]=map[i][j-1];
        from[side]=map[i][j];
        cap[side]=1;
       next[++side]=head[map[i][j-1]];
        head[map[i][j-1]]=side;
        to[side]=map[i][j];
        from[side]=map[i][j-1];
        cap[side]=0;
       }
     }
   
  
        else 
         {
         next[++side]=head[map[i][j]];
  head[map[i][j]]=side;
  to[side]=end;
  from[side]=map[i][j];
  cap[side]=1;
next[++side]=head[end];
head[end]=side;
to[side]=map[i][j];
from[side]=end;
cap[side]=0;
         }
  
}
  }  
}

int h[15001];
bool visited[15001];
int line [15001];
bool BFS()
{
int left,right,l,r,i,j,k;
visited[start]=true;
line[1]=start;
h[start]=1;
left=1;
right=1;
  while(left<=right)
   {
    l=left%(sum+1);
    r=right%(sum+1);
    j=head[line[l]];
    while(j!=0)
     {
     if(!visited[to[j]]&&cap[j]>flow[j])
      {
       h[to[j]]=h[line[l]]+1;
       line[(++right)%(sum+1)]=to[j];
       visited[to[j]]=true;
      }
     j=next[j];
     }
    left++;
   }
return visited[end];
}

int DFS(int x,int now)
 {
  int a=now;
    int i,j,k,l;
    j=head[x];
    if(x==end||now==0)
     returnnow;
    while(j!=0)
    {
      if(cap[j]>flow[j]&&h[to[j]]==h[from[j]]+1)
 { l=DFS(to[j],min(a,cap[j]-flow[j])) ;
     a-=l;
     flow[j]+=l;
     flow[j^1]-=l;
     if(a==0)
      return now; 
 }  
 j=next[j];
    }
  return now-a;
 }

void find()
{
int ans=0;
while(BFS()) 
{
ans+=DFS(start,INF);
memset(visited,false,sizeof(visited));
memset(h,0,sizeof(h));
memset(line,0,sizeof(line));
}
printf("%d\n",ans);
}

int main()
{
int i,k;

   
       read();
find();

return 0;
}

0 0