码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 3020 Antenna Placement(二分匹配+拆点)

时间:2014-09-10 15:49:50      阅读:216      评论:0      收藏:0      [点我收藏+]

标签:poj   二分匹配   

题目链接:http://poj.org/problem?id=3020


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. 
bubuko.com,布布扣 
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


题意:

一个矩形图中,有N个城市‘*’,这n个城市都要覆盖无线网络,若放置一个基站,那么它至多可以覆盖相邻的两个城市。
问最少需要放置多少个基站才能使所有的城市都覆盖无线?

PS:把原来的有向图G的每一个顶点都拆分为2个点,分别属于所要构造的二分图的两个顶点集里!


代码如下:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
#define MAXN 517
int N;
int g[MAXN][MAXN], mm[MAXN][MAXN];
int linker[MAXN];
bool used[MAXN];
int dx[4] = {-1,1,0,0};
int dy[4] = {0,0,1,-1};//四个方向
int dfs(int L)//从左边开始找增广路径
{
    for(int R = 1 ; R <= N ; R++ )
    {
        if(g[L][R] && !used[R])
        {
            //找增广路,反向
            used[R]=true;
            if(linker[R] == -1 || dfs(linker[R]))
            {
                linker[R]=L;
                return 1;
            }
        }
    }
    return 0;
}
int hungary()
{
    int res = 0 ;
    memset(linker,-1,sizeof(linker));
    for(int L = 1; L <= N; L++)
    {
        memset(used,0,sizeof(used));
        if(dfs(L))
            res++;
    }
    return res;
}

void init()
{
    memset(g,0,sizeof(g));
    memset(mm,0,sizeof(mm));
}
int main()
{
    int t;
    char tt;
    int n, m;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        init();
        int p = 0;
        for(int i = 1; i <= n; i++)
        {
            getchar();
            for(int j = 1; j <= m; j++)
            {
                scanf("%c",&tt);
                if(tt == '*')
                    mm[i][j] = ++p;
            }
        }
        for(int i = 1; i <= n; i++)
        {
            for(int j = 1; j <= m; j++)
            {
                if(mm[i][j])
                {
                    for(int k = 0; k < 4; k++)
                    {
                        int x = i+dx[k];
                        int y = j+dy[k];
                        if(mm[x][y])
                            g[mm[i][j]][mm[x][y]] = 1;
                    }
                }
            }
        }
        N = p;
        int ans = p - hungary()/2;
        printf("%d\n",ans);
    }
    return 0;
}



POJ 3020 Antenna Placement(二分匹配+拆点)

标签:poj   二分匹配   

原文地址:http://blog.csdn.net/u012860063/article/details/39181831

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!