POJ 3020 Antenna Placement 匈牙利算法变形

来源:互联网 发布:js提示return非法 编辑:程序博客网 时间:2024/05/17 08:32

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

27 9ooo**oooo**oo*ooo*o*oo**o**ooooooooo*******ooo*o*oo*oo*******oo10 1***o******

Sample Output

175

题意:‘o’代表空地,‘*’代表城市,在城市中建立信号站(?),每个能覆盖建信号站的城市和相邻的一个城市(仅能覆盖另外一个,即最多覆盖两个)。问,最少需要建几个信号站。


方法:这题是一道变形的匈牙利算法。和明显,一个信号站只能覆盖自身与相邻的一个城市,既将两个城市配对。如果总共有n座城市,m组配对,那么需要建设的信号站总数就是n-m。


注意。既然是匈牙利算法,那就需要建立一张图。建立图的方法既将相邻的两座城市用一条无向边(也就是双向边)连起来,这样一来,在统计配对数m‘时,每组配对计算了两次(即a,b配对时,b也与a配对),m'=2*m。所以,最后的答案就是n-m/2.


代码:

//By Sean Chen#include <iostream>#include <cstdio>#include <cstring>using namespace std;int Map[405][405],data[45][15],used[405],a[405];char in[45][10];int dir[4][2]={0,1,0,-1,-1,0,1,0};          //四个方向,找相邻城市int T,n,m,cnt;void init(){    cnt=0;                     //统计城市数,并给每个城市编号    for (int i=0;i<n;i++)    {        scanf("%s",in[i]);        for (int j=0;j<m;j++)        {            if (in[i][j]=='*')                data[i][j]=++cnt;            else                data[i][j]=0;        }    }    return;}void buildmap(){    for (int i=0;i<n;i++)        for (int j=0;j<m;j++)        {            if (data[i][j])            {                for (int k=0;k<4;k++)                {                    int i1=i+dir[k][0],j1=j+dir[k][1];                    if (i1>=0 && j1>=0 && i1<n && j1<m && data[i1][j1])          //双向建图                    {                        Map[data[i][j]][data[i1][j1]]=1;                        Map[data[i1][j1]][data[i][j]]=1;                    }                }            }        }    return;}int findans(int pos){    for (int i=1;i<=cnt;i++)    {        if (Map[pos][i] && !used[i])        {            used[i]=1;            if (!a[i] || findans(a[i]))            {                a[i]=pos;                return 1;            }        }    }    return 0;}int main(){    scanf("%d",&T);    while (T--)    {        memset(a,0,sizeof(a));        memset(data,0,sizeof(data));        memset(Map,0,sizeof(Map));        scanf("%d%d",&n,&m);        if (!m)        {            printf("0\n");            continue;        }        init();        buildmap();        int ans=0;        for (int i=1;i<=cnt;i++)        {            memset(used,0,sizeof(used));            ans+=findans(i);        }        printf("%d\n",cnt-ans/2);    }    return 0;}


0 0
原创粉丝点击