您的位置:首页 > 其它

H - Antenna Placement(匈牙利算法+拆点+无向二分图建立)

2017-10-12 19:55 176 查看
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


为了把有向图G构造为无向二分图,这里需要引入一个新名词“拆点”

其实就是把原有向图G的每一个顶点都”拆分(我认为复制更准确)”为2个点,分别属于所要构造的二分图的两个顶点集

最大二分匹配书之所以要除以2,是因为进行了“拆点”擦奥做做使得匹配总数多了一倍,因此除以2得到原图的真正的匹配数


#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
using namespace std;
char ma[400][400];
int kma[400][400];
bool pma[400][400];
bool v[434];
int link[421];
int n, m, k, M;
int xx[] = {0, 1, -1, 0};
int yy[] = {1, 0, 0, -1};
bool dfs(int x)
{
for(int y=1;y<=k;y++)
{
if(pma[x][y]&&!v[y])
{
v[y] = true;
if(link[y]==0||dfs(link[y]))
{
link[y] = x;
return true;
}
}
}
return false;
}
void Search()
{
for(int x = 1;x<=k;x++)
{
memset(v, false, sizeof(v));
if(dfs(x))
M++;
}
return ;
}
int main()
{
int T;
scanf("%d", &T);
while(T--)
{
scanf("%d %d", &n, &m);
memset(link, 0, sizeof(link));
k = 0;
M = 0;
for(int i=0;i<n;i++)
{
scanf("%s", ma[i]);//存图
}
memset(kma, 0, sizeof(kma));
memset(pma, false, sizeof(pma));
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(ma[i][j]=='*')
kma[i][j] = ++k;//建立需要匹配的图
}
}
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(kma[i][j])//匹配
{
for(int ka = 0; ka<4;ka++)
{
int x = xx[ka] + i;
int y = yy[ka] + j;
if(kma[x][y])
{
pma[kma[i][j]][kma[x][y]] = true;
}
}
}
}
}
Search();
printf("%d\n", k-M/2);//最小路径覆盖=G-最大匹配数
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: