Antenna Placement poj 3020 二分图 最小覆盖

Antenna Placement poj 3020

Antenna Placement

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 12104 Accepted: 5954

Description

The Global Aerial Research Centre has been allotted the task of building the fifth generation of mobile phone nets in Sweden. The most striking reason why they got the job, is their discovery of a new, highly noise resistant, antenna. It is called 4DAir, and comes in four types. Each type can only transmit and receive signals in a direction aligned with a (slightly skewed) latitudinal and longitudinal grid, because of the interacting electromagnetic field of the earth. The four types correspond to antennas operating in the directions north, west, south, and east, respectively. Below is an example picture of places of interest, depicted by twelve small rings, and nine 4DAir antennas depicted by ellipses covering them.

在这里插入图片描述

Obviously, it is desirable to use as few antennas as possible, but still provide coverage for each place of interest. We model the problem as follows: Let A be a rectangular matrix describing the surface of Sweden, where an entry of A either is a point of interest, which must be covered by at least one antenna, or empty space. Antennas can only be positioned at an entry in A. When an antenna is placed at row r and column c, this entry is considered covered, but also one of the neighbouring entries (c+1,r),(c,r+1),(c-1,r), or (c,r-1), is covered depending on the type chosen for this particular antenna. What is the least number of antennas for which there exists a placement in A such that all points of interest are covered?

Input

On the first row of input is a single positive integer n, specifying the number of scenarios that follow. Each scenario begins with a row containing two positive integers h and w, with 1 <= h <= 40 and 0 < w <= 10. Thereafter is a matrix presented, describing the points of interest in Sweden in the form of h lines, each containing w characters from the set [’’,‘o’]. A '’-character symbolises a point of interest, whereas a ‘o’-character represents open space.

Output

For each scenario, output the minimum number of antennas necessary to cover all ‘*’-entries in the scenario’s matrix, on a row of its own.

Sample Input

2
7 9
ooooooo
ooooo
o*oo
o

ooooooooo
****oo
o
o
oo
oo
*******oo
10 1
*
*
*
o
*
*
*
*
*
*
Sample Output

17
5

Source

Svenskt Mästerskap i Programmering/Norgesmesterskapet 2001

// 题意:给一张图,图中有两种点,一次只能覆盖相邻(上下左右)两点
// 求最小覆盖数
// 二分图最小覆盖数等于最大匹配数,匈牙利算法求最大匹配数
// 此题建图方面有些繁琐,看了大佬的题解后,才建图成功


#include <cstdio>
#include <cstring>

using namespace std;

const int max_h = 44;
const int max_w = 14;

int h,w;
char s[max_h][max_w];

int n;
int total=0,save=0,cur;
int direct[4][2]={{1,0},{-1,0},{0,1},{0,-1}};

const int max_n=500+5;
int cx[max_n],cy[max_n],st[max_n];
bool vis[max_n];

struct node
{
    int y,nxt;
};
// 这里数组开的小了点,但是刚好够用了,大佬在题解中开了比这大百倍的数组。不知道为什么
// 这个数组的最大用量,应该由add函数的使用上限决定
// 每遍历一个点,最多在上下左右四个方向进行一次add函数的调用,所以只需要4*max_n次即可
node way[2000];

// 计算在一维数组中的位置
int get(int i,int j)
{
    return i*w+j;
}

void add(int u,int v)
{
    ++cur;
    // 当前数组中存储邻接点v和st【u】
    way[cur].y=v;
    way[cur].nxt=st[u];
    // st数组存储当u的边在数组中的位置
    st[u]=cur;
}

int match(int x)
{
    for(int i=st[x];i;i=way[i].nxt)
    {
        int y=way[i].y;
        if(!vis[y])
        {
            vis[y]=1;
            if(!cy[y] || match(cy[y]))
            {
                cx[x]=y;
                cy[y]=x;
                return 1;
            }
        }
    }
    return 0;
}

int XYL()
{
    memset(cx,0,sizeof(cx));
    memset(cy,0,sizeof(cy));
    int ans=0;
    for(int i=0;i<n;++i)
    {
        // 遍历所有节点,如果找到没有匹配的x,看能否找到与之匹配的y
        // 这里加了一步判断,对st[i]不为0的判断,也就是对当前节点不为'o'的判断
        // 不加这一个判断也可得出正确的结果,但加入后会有优化,不用再执行之后许多无用的操作
        // 毕竟只有'*'的点需要匹配不是吗?
        if(!cx[i] && st[i])
        {
            memset(vis,0,sizeof(vis));
            ans+=match(i);
        }
    }
    return ans;
}

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        // 输入数据
        scanf("%d %d",&h,&w);
        for(int i=0;i<h;++i)
        {
            scanf("%s",s[i]);
        }

        // 初始化way数组和st数组
        cur=0;
        memset(st,0,sizeof(st));
        // n表示总节点数
        n=h*w;
        // total表示*的总数
        total=0;


        for(int i=0;i<h;++i)
        {
            for(int j=0;j<w;++j)
            {
                if(s[i][j]=='*')
                {
                    ++ total;
                    // 检查相邻四个方向
                    for(int k=0;k<4;++k)
                    {
                        int tx=i+direct[k][0];
                        int ty=j+direct[k][1];
                        // 在图的范围内且为*时,加边
                        if(tx>=0 && tx<h && ty>=0 && ty<w)
                        {
                            if(s[tx][ty]=='*')
                            {
                                int u=get(i,j);
                                int v=get(tx,ty);
                                add(u,v);
                                // printf("add");
                            }
                        }
                    }
                }
            }
        }

        //printf("cur:%d\n",tot);

        printf("%d\n",total-XYL()/2);
    }
    return 0;
}

/*
2
7 9
ooo**oooo
**oo*ooo*
o*oo**o**
ooooooooo
*******oo
o*o*oo*oo
*******oo
10 1
*
*
*
o
*
*
*
*
*
*
*/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值