POJ 3020 Antenna Placement 【最小边覆盖】

传送门:http://poj.org/problem?id=3020

Antenna Placement
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 11098   Accepted: 5464

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

题意概括:

给一个高为 H 宽为 W 的图案, “  *  ” 表示城市,每个城市可以覆盖 它 上下左右相邻的 其中一个城市,问最好需要多少城市才能把所有城市覆盖。

解题思路:

按照城市编号,然后拆点建图,相邻两个城市可以覆盖的就连边,跑一遍最小边覆盖。

AC code:

 1 #include <cstdio>
 2 #include <iostream>
 3 #include <cstring>
 4 #include <cmath>
 5 #define INF 0x3f3f3f3f
 6 using namespace std;
 7 const int MAXH = 45;
 8 const int MAXW = 15;
 9 const int MAXN = 500;
10 struct Edge
11 {
12     int v, nxt;
13 }edge[MAXN*MAXN];
14 int head[MAXN], cnt;
15 int linker[MAXN];
16 bool used[MAXN];
17 char str[MAXH][MAXW];
18 int a[MAXH][MAXW];
19 int sum;
20 int H, W;
21 
22 void init()
23 {
24     memset(head, -1, sizeof(head));
25     memset(linker, -1, sizeof(linker));
26     memset(a, 0, sizeof(a));
27     cnt = 0;
28     sum = 0;
29 }
30 
31 void add(int from, int to)
32 {
33     edge[cnt].v = to;
34     edge[cnt].nxt = head[from];
35     head[from] = cnt++;
36 }
37 
38 bool Find(int x)
39 {
40     int v;
41     for(int i = head[x]; i != -1; i = edge[i].nxt){
42         v = edge[i].v;
43         if(!used[v]){
44             used[v] = true;
45             if(linker[v] == -1 || Find(linker[v])){
46                 linker[v] = x;
47                 return true;
48             }
49         }
50     }
51     return false;
52 }
53 
54 int main()
55 {
56     int T_case;
57     scanf("%d", &T_case);
58     while(T_case--){
59         init();
60         scanf("%d%d", &H, &W);
61         for(int i = 0; i < H; i++){
62             scanf("%s", &str[i]);
63             for(int j = 0; j < W; j++)
64                 if(str[i][j]=='*') a[i][j] = ++sum;
65         }
66 
67         for(int i = 0; i < H; i++){
68             for(int j = 0; j < W; j++){
69                 if(a[i][j]){
70                     if(i > 0 && a[i-1][j]) add(a[i][j], a[i-1][j]);
71                     if(i < H-1 && a[i+1][j]) add(a[i][j], a[i+1][j]);
72                     if(j > 0 && a[i][j-1]) add(a[i][j], a[i][j-1]);
73                     if(j < W-1 && a[i][j+1]) add(a[i][j], a[i][j+1]);
74                 }
75             }
76         }
77 
78         int ans = 0;
79         for(int i = 1; i <= sum; i++){
80             memset(used, 0, sizeof(used));
81             if(Find(i)) ans++;
82         }
83         printf("%d\n", sum-ans/2);
84     }
85     return 0;
86 }
View Code

猜你喜欢

转载自www.cnblogs.com/ymzjj/p/10001332.html