POJ 3020 Antenna Placement (Hungary)

 求最小覆盖边,

 无向二分图的最小路径覆盖 = 顶点数 – 最大二分匹配数/2

 由于构图过程中匹配双向的,所以匹配数多了一倍,最后要/2

 构图可以用奇偶,或者用数来标记

//#pragma comment(linker, "/STACK:102400000,102400000")
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
typedef long long LL;
const long long mod = 1e9 + 7;
const int INF = 0x3f3f3f3f;
//const int M = 305;

struct node
{
    int s,t,nxt ;
} e[5000] ;
int m,n,head[405],cnt,match[405],vis[405];

int find(int s)
{
    for(int i=head[s] ; i!=-1 ; i=e[i].nxt)
    {
        int tt=e[i].t ;
        if(!vis[tt])
        {
            vis[tt]=1 ;
            if(match[tt]==-1 || find(match[tt]))
            {
                match[tt]=s ;
                return 1 ;
            }
        }
    }
    return 0 ;
}

int max_match()
{
    int ans=0 ;
    memset(match,-1,sizeof(match)) ;
    for(int i=1 ; i<=m ; i++)
    {
        memset(vis,0,sizeof(vis)) ;
        ans+=find(i);
    }
    return ans;
}

void add(int s,int t)
{
    e[cnt].s=s ;
    e[cnt].t=t ;
    e[cnt].nxt=head[s] ;
    head[s]=cnt++ ;
}

//char M[405][405] ;
int mp[455][455] ;
int dx[]= {1,-1,0,0} ;
int dy[]= {0,0,1,-1} ;

int main()
{
    int T ;
    scanf("%d",&T) ;
    while(T--)
    {
        int N,M;
        scanf("%d%d",&N,&M) ;
        n=0 ;
        char tmp;
        memset(mp,0,sizeof(mp)) ;
        for(int i=0 ; i<N ; i++)
        {
            for(int j=0 ; j<M ; j++)
            {
                cin>>tmp;
                if(tmp=='*')
                {
                    mp[i][j]=++n;
                }
                       //也可以用奇偶数建图
//                if(tmp=='*')
//                {
//                    if((i+j)&1)//奇点
//                        mp[i][j]=++ln;
//                    else//偶点
//                        mp[i][j]=++rn;
//                }
//                else
//                    mp[i][j]=0;
            }
        }
        memset(head,-1,sizeof(head)) ;
        cnt=0 ;
        for(int i=0 ; i<N ; i++)
        {
            for(int j=0 ; j<M ; j++)
            {
                if(mp[i][j])
                {
                    for(int k=0 ; k<4 ; k++)
                    {
                        int xx=i+dx[k] ;
                        int yy=j+dy[k] ;
                        if(xx<0 || xx>=N || yy<0 || yy>=M || !mp[xx][yy])
                            continue ;
                        if(mp[xx][yy])
                        {
                            add(mp[i][j],mp[xx][yy]) ;
                        }
                    }
                }
            }
        }
        m=n ;
        printf("%d\n",n-max_match()/2) ;
    }
    return 0 ;
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值