您的位置:首页 > 其它

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

2015-08-08 19:47 585 查看
Antenna Placement

Time Limit: 1000MSMemory Limit: 65536K
Total Submissions: 7517Accepted: 3729
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

    题目大意:
*代表城市O为其他不可经过的路。求将所以的城市*覆盖需要至少需要多少网站点来覆盖。(前提是每个网站点可以覆盖相邻的两个*)。
类似打怪游戏,上二分图。打怪游戏(上篇)求最少开多少枪将怪全部消灭。因为给出了每个怪的位置,将怪看作链接每个X,Y轴的链接线(linkmap),所以建图极为容易
.
   但是此题,能不能也是直接这么做呢,我想不少人会直接用lingkmap[i][j]=true.但想想这根本不是链接的两个点。由题目可知此为无向图,所以要”拆点“来建图。所谓拆点即是
对点的复制。
如:
    


 
然后,统计图中*的个数然后编号。根据当前点的编号,和向上下左右搜到的可链接的点进行建图。
因为是对构造出的点进行覆盖,所以要用最小路径覆盖。而打怪游戏,怪物充当的是边,所以用最小点覆盖。
之后 最小路径的覆盖=点数-最大二分匹配/2.

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define inf 0x3f3f3f3f
using namespace std;
int linkmap[604][604],tmp[604][604];int cropath[604],use[604];
char Map[604][604];
int _r[]={0,-1,0,1},_c[]={1,-0,-1,0};
int n,m,t,s,tol,point;
bool fi(int u)
{
    int i,j,k;
    for(i=1;i<=point;i++)
    {
        if(linkmap[u][i]==1&&!use[i])
        {
            use[i]=1;
            if(cropath[i]==-1||fi(cropath[i]))
            {
                cropath[i]=u;
                return true;
            }
        }
    }
    return false;
}
int main()
{
    int cla,n,m,i,j,k;
    scanf("%d",&cla);
    while(cla--)
    {
        point=tol=0;
        memset(linkmap,0,sizeof(linkmap));
        memset(cropath,-1,sizeof(cropath));
        memset(tmp,0,sizeof(tmp));
        scanf("%d%d",&n,&m);
        for(i=0;i<n;i++)
        {
            scanf("%s",Map[i]);
        }
        for(i=0;i<n;i++)
        {
            for(j=0;j<m;j++)
            {
                if(Map[i][j]=='*')
                {
                    tmp[i][j]=++point;
                }
            }
        }

        for(i=0;i<n;i++)
        {
            for(j=0;j<m;j++)
            {
                if(tmp[i][j])
                {
                    for(k=0;k<4;k++)
                    {
                        int x=i+_r[k];
                        int y=j+_c[k];
                        linkmap[ tmp[i][j]  ][ tmp[x][y] ]=1;
                    }
                }
            }
        }
        int ans=0;
        for(i=1;i<=point;i++)
        {
            if(fi(i))
            {
                memset(use,0,sizeof(use));
                ans++;
            }
        }
        printf("%d\n",point-ans/2);
    }
    return 0;
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: