poj 3020 Antenna Placement (二分图最小路径覆盖)

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<queue>
#include<stack>
#include<list>
#include<map>
#include<set>
#include<vector>

typedef long long int ll;
const int mod =1e9+7;
const int INF=0x3f3f3f3f;
using namespace std;
const int MAXN=510;
int uN,vN;//u,v数目
int g[MAXN][MAXN];
int linker[MAXN];
bool used[MAXN];
bool dfs(int u)
{
    int v;
    for(v=0;v<vN;v++)
      if(g[u][v]&&!used[v])
      {
          used[v]=true;
          if(linker[v]==-1||dfs(linker[v]))
          {
              linker[v]=u;
              return true;
          }
      }
    return false;
}
int hungary()
{
    int res=0;
    int u;
    memset(linker,-1,sizeof(linker));
    for(u=0;u<uN;u++)
    {
        memset(used,0,sizeof(used));
        if(dfs(u)) res++;
    }
    return res;
}
char map[50][50];
int hash[50][50];
int main()
{
    int T;
    int h,w;
    scanf("%d",&T);
    int tol;
    while(T--)
    {
        scanf("%d%d",&h,&w);
        tol=0;
        for(int i=0;i<h;i++)
        {
            scanf("%s",&map[i]);
            for(int j=0;j<w;j++)
             if(map[i][j]=='*')
               hash[i][j]=tol++;
        }
        memset(g,0,sizeof(g));
        for(int i=0;i<h;i++)
          for(int j=0;j<w;j++)
            if(map[i][j]=='*')
            {
                if(i>0&&map[i-1][j]=='*')g[hash[i][j]][hash[i-1][j]]=1;
                if(i<h-1&&map[i+1][j]=='*') g[hash[i][j]][hash[i+1][j]]=1;
                if(j>0&&map[i][j-1]=='*')  g[hash[i][j]][hash[i][j-1]]=1;
                if(j<w-1&&map[i][j+1]=='*')  g[hash[i][j]][hash[i][j+1]]=1;
            }
        uN=vN=tol;
        printf("%d\n",tol-hungary()/2);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/wzazzy/article/details/83339288