Antenna Placement
Time Limit: 1000MS |
|
Memory Limit: 65536K |
Total Submissions: 6458 |
|
Accepted: 3192 |
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
该题目本质上仍然考察二分图的最大匹配,关键在于题意的理解转化成最小边覆盖以及二分图的构建。
基本定义:
边覆盖集:通俗地讲,所谓边覆盖集,就是G中所有的顶点都是E*中某条边的邻接顶点(边覆盖顶点),一条边只能覆盖2个顶点。
注意:在无向图中存在用尽量少的边去“覆盖”住所有的顶点,所以边覆盖集有极小与最小的区别。
极小边覆盖:若边覆盖E*中的任何真子集都不是边覆盖集,则称E*是极小边覆盖集。
最小边覆盖:边数最小的边覆盖称为最小边覆盖,通俗地讲,就是极小边覆盖中的最小的一个集合。
最小边覆盖满足:任何一个顶点有且只有一条路径与之关联,即如果把这些路径中的每条路径从它的起始点走到它的终点,那么恰好可以经过图中的每个顶点一次且仅一次。
最小边覆盖在二分图中的应用:
最小边覆盖 = 最大独立集 = n - 最大匹配,这个是二分图上的一个性质。
最小路径覆盖与最小边覆盖的区别
最小路径覆盖和最小边覆盖不同,不要求给的图是二分图,而是要求是PXP的有向图,不能有环,即有向无环图。然后根据原图构造二分图,构造方法是将点一分为二(拆点),v分为v*和v**然后如v*和u**有边,那么就在v*和u**之间连一条边。然后最小路径覆盖是n-m,n为原图的点的个数,m为新造二分图的最大匹配。
在该题目中,每个卫星可以覆盖东南西北任选一个方向和本身两个点,那么把所有需要覆盖的点编号作为构建的新图的点,每个点东南西北可以覆盖一个点,如果A点东边相邻B点,那么在新图中,有A、B点之间有一条边。这样就能够成一个新的无向图,问题就转化成:求无向图的最小边覆盖。将新图中的每个点P分成两个点p1和p2,分别位于X点集和Y点集。那么AB之间的路径,就转化成A1B2和A2B1的两条路径,那么通过匈牙利算法计算出的最大匹配是双倍的,要进行除以2才是真正的最大匹配数。因此,无向图的最小边匹配公式= n - 最大匹配。
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int n,h,w,tu[45][15],count,a[500][500],use[500],result[500],number;
char temh[15];
void sou(int x,int y)
{
if(y+1<=w&&tu[x][y+1])
//a[tu[x][y]][tu[x][y+1]]=1;
a[tu[x][y]][tu[x][y+1]]=a[tu[x][y+1]][tu[x][y]]=1;
if(x+1<=h&&tu[x+1][y])
//a[tu[x][y]][tu[x+1][y]]=1;
a[tu[x][y]][tu[x+1][y]]=a[tu[x+1][y]][tu[x][y]]=1;
}
void zhitu()
{
int i,j;
for(i=1;i<=h;i++)
{
for(j=1;j<=w;j++)
{
if(tu[i][j])
sou(i,j);
}
}
}
void init()
{
int i,j;
int k=1;
scanf("%d%d",&h,&w);
for(i=1;i<=h;i++)
{
scanf("%s",temh);
for(j=0;j<w;j++)
if(temh[j]=='o')
tu[i][j+1]=0;
else
if(temh[j]=='*')
tu[i][j+1]=k++;
}
count=k-1;
memset(a,0,sizeof(a));
memset(use,0,sizeof(use));
memset(result,0,sizeof(result));
zhitu();
number=0;
}
int find(int x)
{
int i,j;
for(i=1;i<=count;i++)
{
if(a[x][i]&&!use[i])
{
use[i]=1;
if(!result[i]||find(result[i]))
{
result[i]=x;
return 1;
}
}
}
return 0;
}
int main()
{
int i,j;
scanf("%d",&n);
while(n--)
{
init();
for(i=1;i<=count;i++)
{
memset(use,0,sizeof(use));
if(find(i))
number++;
}
printf("%d\n",count-number/2);
}
return 0;
}