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?
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
题意:‘*’假设为城市,每两个城市之间需要有信号覆盖每个城市可以与与其相邻的上下左右进行信号覆盖。最终求覆盖所有城市需要的最少的覆盖圈个数。
每个城市相互之间都可以进行覆盖,所以此题需要建立无向图,而城市为两两建立联系,自然需要使用二分图。该题就是求无向二分图的最小路径覆盖。而无向二分图最小路径覆盖=顶点数-最大匹配数/2;用匈牙利算法求出最大匹配得解。该题关键在建图,将所有‘*’标号。每个‘*’之间建立无向联系即可。
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define INF 0x3f3f3f3f
using namespace std;
int map[502][502];
int map_p[500][500];
int link[502];
int used[502];
int ans,n,sum,k;
int dx[]={-1,1,0,0},dy[]={0,0,-1,1};
int DFS(int u)
{
int i,j;
for (int v=1;v<k;v++)
{
if(!used[v]&&map[u][v])
{
used[v]=1;
if(!link[v]||DFS(link[v]))
{
link[v]=u;
return 1;
}
}
}
return 0;
}
void maxmatch()
{
sum=0;
memset(link,0,sizeof(link));
for (int u=1;u<k;u++)
{
memset(used,0,sizeof(used));
if(DFS(u)) sum++;
}
}
int main()
{
int x,y;
int t,i,j,l;
char c;
cin>>t;
while(t--)
{
k=1;
memset(map_p,0,sizeof(map_p));
memset(map,0,sizeof(map));
cin>>x>>y;
for (i=1;i<=x;i++)
{
for (j=1;j<=y;j++)
{
cin>>c;
if(c=='*') map_p[i][j]=k++;
}
}
for (i=1;i<=x;i++)
{
for (j=1;j<=y;j++)
{
if(map_p[i][j])
{
for (l=0;l<3;l++)
if(map_p[i+dx[l]][j+dy[l]]){
map[map_p[i][j]][map_p[i+dx[l]][j+dy[l]]]=1;
map[map_p[i+dx[l]][j+dy[l]]][map_p[i][j]]=1;
}
}
}
}
maxmatch();
ans=k-1-sum/2;
cout<<ans<<endl;
}
return 0;
}