hdu 4185 Oil Skimming 二分匹配

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
const int MAXN=601*601+10;
const int MAXM=601*601*2;
struct Edge
{
    int to,next;
} edge[MAXM];
int head[MAXN],tot,mx[MAXN],cx[MAXN],cy[MAXN],mk[MAXN];
char mp[1024][1024];
int nums[1024][1024];

void addedge(int from,int to)
{
    edge[tot].to=to;
    edge[tot].next=head[from];
    head[from]=tot++;
}

void init()
{
    memset(head,0xff,sizeof(head));
    tot=0;
}

int dfs(int u)
{
    for(int i=head[u]; i!=-1; i=edge[i].next)
    {
        int v=edge[i].to;
        if(!mk[v])
        {
            mk[v]=1;
            if(cy[v]==-1||dfs(cy[v]))
            {
                cx[u]=v;
                cy[v]=u;
                return 1;
            }
        }
    }
    return 0;
}

int Maxmatch(int n)
{
    int res=0;
    memset(cx,0xff,sizeof(cx));
    memset(cy,0xff,sizeof(cy));
    for(int i=1; i<=n; i++)
    {
        if(cx[i]==-1)
        {
            memset(mk,0,sizeof(mk));
            res+=dfs(i);
        }
    }
    return res;
}

int main()
{
    int _,n,m,i,j,cas=0,num;
    scanf("%d",&_);
    while(_--)
    {
        init();
        memset(nums,0,sizeof(nums));
        num=0;
        scanf("%d",&n);

        for(i=0; i<n; i++)
        {
            scanf("%s",mp[i]);
            for(j=0; j<n; j++)
            {
                if(mp[i][j]=='#')
                    nums[i][j]=++num;
            }
        }

        for(i=0; i<n; i++)
        {
            for(j=0; j<n; j++)
            {
                if(mp[i][j]!='#') continue;
                if(mp[i+1][j]=='#'&&i+1<n)
                {
                    addedge(nums[i][j],nums[i+1][j]);
                    addedge(nums[i+1][j],nums[i][j]);
                }
                if(mp[i][j+1]=='#'&&j+1<n)
                {
                    addedge(nums[i][j],nums[i][j+1]);
                    addedge(nums[i][j+1],nums[i][j]);
                }
            }
        }
        printf("Case %d: %d\n",++cas,Maxmatch(num)/2);
    }
    return 0;
}
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
const int MAXN=800;
const int INF=1<<28;
int g[MAXN][MAXN],Mx[MAXN],My[MAXN],Nx,Ny,nums[1024][1024];
int dx[MAXN],dy[MAXN],dis;
char mp[1024][1024];
bool vst[MAXN];

bool searchP()
{
    queue<int>Q;
    dis=INF;
    memset(dx,-1,sizeof(dx));
    memset(dy,-1,sizeof(dy));
    for(int i=1;i<=Nx;i++)
        if(Mx[i]==-1)
        {
            Q.push(i);
            dx[i]=0;
        }
    while(!Q.empty())
    {
        int u=Q.front();
        Q.pop();
        if(dx[u]>dis)  break;
        for(int v=1;v<=Ny;v++)
            if(g[u][v]&&dy[v]==-1)
            {
                dy[v]=dx[u]+1;
                if(My[v]==-1)  dis=dy[v];
                else
                {
                    dx[My[v]]=dy[v]+1;
                    Q.push(My[v]);
                }
            }
    }
    return dis!=INF;
}
bool DFS(int u)
{
    for(int v=1;v<=Ny;v++)
       if(!vst[v]&&g[u][v]&&dy[v]==dx[u]+1)
       {
           vst[v]=1;
           if(My[v]!=-1&&dy[v]==dis) continue;
           if(My[v]==-1||DFS(My[v]))
           {
               My[v]=u;
               Mx[u]=v;
               return 1;
           }
       }
    return 0;
}
int MaxMatch()
{
    int res=0;
    memset(Mx,-1,sizeof(Mx));
    memset(My,-1,sizeof(My));
    while(searchP())
    {
        memset(vst,0,sizeof(vst));
        for(int i=1;i<=Nx;i++)
          if(Mx[i]==-1&&DFS(i))  res++;
    }
    return res;
}


int main()
{
    int _,n,m,i,j,cas=0,num;
    scanf("%d",&_);
    while(_--)
    {
        memset(g,0,sizeof(g));
        memset(nums,0,sizeof(nums));
        num=0;
        scanf("%d",&n);
        for(i=0;i<n;i++)
        {
            scanf("%s",mp[i]);
            for(j=0;j<n;j++)
            {
                if(mp[i][j]=='#')
                    nums[i][j]=++num;
            }
        }
        for(i=0;i<n;i++)
        {
            for(j=0;j<n;j++)
            {
                if(mp[i][j]!='#') continue;
                if(mp[i+1][j]=='#'&&i+1<n)
                {
                    g[nums[i][j]][nums[i+1][j]]=1;
                    g[nums[i+1][j]][nums[i][j]]=1;
                    //printf("%d %d\n",nums[i][j],nums[i+1][j]);
                }

                if(mp[i][j+1]=='#'&&j+1<n)
                {
                    g[nums[i][j]][nums[i][j+1]]=1;
                    g[nums[i][j+1]][nums[i][j]]=1;
                    //printf("%d %d\n",nums[i][j],nums[i][j+1]);
                }
            }
        }
        Nx=Ny=num;
        printf("Case %d: %d\n",++cas,MaxMatch()/2);
    }
    return 0;
}

你可能感兴趣的:(hdu 4185 Oil Skimming 二分匹配)