poj 3020 -- Antenna Placement
Antenna Placement
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 6346 | Accepted: 3132 |
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
思路:二分匹配的题。
可以用 i×m+j 来对星进行编号,将每个星的位置存到node数组里。然后判断星号的上下左右是否是星号,如果是则建一条边,即g[][] = 1。建图工作就是这样。假设共有cnt个星。对cnt个星进行二分匹配
则有如下推断:
因为建的是有向边,所以所得的最大匹配数ans 为 真正匹配数的二倍,所以真正匹配数 = ans / 2 ,
此题所求 = 真正匹配数 + 未匹配。 未匹配数 = 星的个数cnt - 真正匹配×2 = cnt - ans
故 此题所求 = ans/2 + cnt - ans = cnt - ans/2 。
推断结束。
1 /*====================================================================== 2 * Author : kevin 3 * Filename : AnernnaPlacement.cpp 4 * Creat time : 2014-07-13 11:25 5 * Description : 6 ========================================================================*/ 7 #include <iostream> 8 #include <algorithm> 9 #include <cstdio> 10 #include <cstring> 11 #include <queue> 12 #include <cmath> 13 #define clr(a,b) memset(a,b,sizeof(a)) 14 #define M 405 15 using namespace std; 16 int g[M][M],linker[M],x[M][M],y[M][M]; 17 int used[M],n,m,node[M],cnt; 18 char str[M][M]; 19 int dir[4][2] = {{-1,0},{0,1},{1,0},{0,-1}}; 20 bool DFS(int u) 21 { 22 for(int v = 1; v <= cnt; v++){ 23 if(g[u][node[v]] && !used[node[v]]){ 24 used[node[v]] = 1; 25 if(linker[node[v]] == -1 || DFS(linker[node[v]])){ 26 linker[node[v]] = u; 27 return true; 28 } 29 } 30 } 31 return false; 32 } 33 int MaxMatch() 34 { 35 int res = 0; 36 clr(linker,-1); 37 for(int u = 1; u <= cnt; u++){ 38 clr(used,0); 39 if(DFS(node[u])) res++; 40 } 41 return res; 42 } 43 void buildgrap(int x,int y) 44 { 45 for(int i = 0; i < 4; i++){ 46 int xx = dir[i][0]+x; 47 int yy = dir[i][1]+y; 48 if(xx >= 0 && xx < n && yy >= 0 && yy < m){ 49 if(str[xx][yy] == '*'){ 50 g[x*m+y][xx*m+yy] = 1; 51 } 52 } 53 } 54 } 55 int main(int argc,char *argv[]) 56 { 57 int cas; 58 scanf("%d",&cas); 59 while(cas--){ 60 clr(x,0); 61 clr(y,0); 62 clr(g,0); 63 clr(str,0); 64 scanf("%d%d",&n,&m); 65 for(int i = 0; i < n; i++){ 66 getchar(); 67 scanf("%s",str[i]); 68 } 69 cnt = 0; 70 for(int i = 0; i < n; i++){ 71 for(int j = 0; j < m; j++){ 72 if(str[i][j] == '*'){ 73 cnt++; 74 buildgrap(i,j); 75 node[cnt] = i*m+j; 76 } 77 } 78 } 79 int ans = MaxMatch(); 80 printf("%d\n",cnt - ans/2); 81 } 82 return 0; 83 }
Do one thing , and do it well !