(POJ3020)Antenna Placement 单链的最大匹配<匈牙利算法,最大匹配>

Antenna Placement
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
ooo**oooo
**oo*ooo*
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

题意:
别被图片的圈圈误导了,看清楚题目,’‘是城市,’o’是空地,椭圆的天线覆盖范围要覆盖的是城市’‘,而不是覆盖空地
一个矩形中,有N个城市’*’,现在这n个城市都要覆盖无线,若放置一个基站,那么它至多可以覆盖相邻的两个城市。
问至少放置多少个基站才能使得所有的城市都覆盖无线?

分析:
如果我把每一个城市进行编号,相邻的城市之间就可以进行建边。通过分析我们可以知道,题目的答案是:在这些边中取一些边将所有城市进行匹配,看最少需要多少边,最后没法匹配的城市单独建立基站。
但是这个题目是每个城市之间的边,没法变成二部图,那就相当于是在单链上进行匹配(我自己想的说法),所有我们只需要将匈牙利算法稍作修改即可。

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;

const int maxn = 410;
char mm[45][15];
int e[maxn][maxn];
int n,m,num;
int dir[4][2]={0,1,0,-1,1,0,-1,0};
int cx[maxn],mk[maxn];
vector<int> g[maxn];

//加边
void add_edge(int x,int y)
{
    int tx,ty;
    for(int i=0;i<4;i++)
    {
        tx = x + dir[i][0];
        ty = y + dir[i][1];
        if(tx >= 0 && tx < n && ty >= 0 && ty < m && e[tx][ty] > 0)
        {
            int u = e[x][y];
            int v = e[tx][ty];
            g[u].push_back(v);
            g[v].push_back(u);
        }
    }
}

int path(int u)
{
    for(int i=0;i<g[u].size();i++)
    {
        int v = g[u][i];
        if(!mk[v])
        {
            mk[v] = 1;
            if(cx[v]==-1 || path(cx[v]))
            {
                cx[u] = v;
                cx[v] = u;
                return 1;
            }
        }
    }
    return 0;
}


int MaxMatch()
{
    int res = 0;
    memset(cx,-1,sizeof(cx));
    //memset(cy,-1,sizeof(cy));
    for(int i=1;i<=num;i++)
    {
        if(cx[i]==-1)
        {
            memset(mk,0,sizeof(mk));
            res+=path(i);
        }
    }
    for(int i=1;i<=num;i++)
        if(cx[i]==-1) res++;
    return res;
}

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        num=0;
        memset(e,0,sizeof(e));
        scanf("%d%d",&n,&m);
        for(int i=0;i<n;i++)
        {
            scanf("%s",mm[i]);
            for(int j=0;j<m;j++)
            {
                if(mm[i][j]=='*')
                    e[i][j]=++num;
            }
        }
        for(int i=1;i<=num;i++) g[i].clear();

        //建图
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                if(e[i][j]>0) add_edge(i,j);
            }
        }
        int ans = MaxMatch();
        printf("%d\n",ans);
    }
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值