POJ3020——Antenna Placement(二分图的最大匹配)

Antenna Placement


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

题目大意:

    给定一个矩形,有N个城市'*'。每一个基站可以覆盖一个城市或者相邻的两个城市。

    计算最少的基站数量。

解题思路:

    假设有x1个基站覆盖两个城市,x2个基站覆盖一个城市,显然N=x1+x2。显然是一个二分匹配问题。

    Ans=顶点数(N)-二分匹配的边的数量(每一个边上的两个城市公用一个基站,所以要减去一个城市)。

    因为二分图的两个集合未知,故构建无向图,使两个集合都为全集。这样匹配出来的边的数量会多一倍,除二即可。

    用匈牙利算法求最大匹配即可。

匈牙利模版:

 1 bool dfs(int x)
 2 {
 3     for (int y=1;y<=num;y++) //第二个集合
 4         if (edge[x][y]&&!vis[y])
 5         {
 6             vis[y]=1;
 7             if (link[y]==0||dfs(link[y]))
 8             {
 9                 link[y]=x;
10                 return true; //匹配成功
11             }
12         }
13     return false;
14 }
15 void search()
16 {
17     for (int x=1;x<=num;x++) //第一个集合
18     {
19         memset(vis,false,sizeof(vis));
20         if (dfs(x))
21         ret++; //记录匹配数
22     }
23     return ;
24 }

Code:

 1 /*************************************************************************
 2     > File Name: poj3020.cpp
 3     > Author: Enumz
 4     > Mail: 369372123@qq.com 
 5     > Created Time: 2014年10月19日 星期日 14时15分12秒
 6  ************************************************************************/
 7 #include<iostream>
 8 #include<cstdio>
 9 #include<cstdlib>
10 #include<string>
11 #include<cstring>
12 #include<list>
13 #include<queue>
14 #include<stack>
15 #include<map>
16 #include<set>
17 #include<algorithm>
18 #define MAXN  500
19 using namespace std;
20 int M,N,num,ret;
21 int  city[MAXN][MAXN],link[MAXN];
22 bool edge[MAXN][MAXN],vis[MAXN];
23 void init()
24 {
25     memset(city,0,sizeof(city));
26     memset(edge,false,sizeof(edge));
27     memset(link,0,sizeof(link));
28     num=0;
29     ret=0;
30 }
31 bool dfs(int x)
32 {
33     for (int y=1;y<=num;y++)
34         if (edge[x][y]&&!vis[y])
35         {
36             vis[y]=1;
37             if (link[y]==0||dfs(link[y]))
38             {
39                 link[y]=x;
40                 return true;
41             }
42         }
43     return false;
44 }
45 void search()
46 {
47     for (int x=1;x<=num;x++)
48     {
49         memset(vis,false,sizeof(vis));
50         if (dfs(x))
51         ret++;
52     }
53     return ;
54 }
55 int main()
56 {
57     int T;
58     cin>>T;
59     while (T--)
60     {
61         init();
62         cin>>M>>N;
63         for (int i=1;i<=M;i++)
64             for (int j=1;j<=N;j++)
65             {
66                 char tmp;
67                 cin>>tmp;
68                 if (tmp=='*')
69                     city[i][j]=++num;
70             }
71         int a[4]={0,0,-1,1},b[4]={1,-1,0,0};
72         for (int i=1;i<=M;i++)
73             for (int j=1;j<=N;j++)
74                 if (city[i][j])
75                 for (int k=0;k<=3;k++)
76                 {
77                     int ti=i+a[k],tj=j+b[k];
78                     if (city[ti][tj])
79                         edge[city[ti][tj]][city[i][j]]=true;//构造无向图
80                 }
81         search();
82         //printf("%d\n",ret);
83         printf("%d\n",num-ret/2);
84     }
85     return 0;
86 }

 

posted @ 2014-11-13 12:47  Enumz  阅读(270)  评论(0编辑  收藏  举报