题目链接:https://vjudge.net/problem/POJ-3020

Antenna Placement
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 9995   Accepted: 4939

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

题解:

1.首先为每个“*”编号。然后对于当前的“*”, 如果它的上面有“*”,则在这两个“*”之间连一条边,同理其他三个方向。

2.利用匈牙利算法求出最大匹配数cnt,即表明最多有cnt个“*”可以与其他“*”共用,所以最少需要N-cnt个。

3.其实此题求的就是最小边覆盖:最小边覆盖 = 结点数 - 最大匹配数

代码如下:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <sstream>
#include <algorithm>
using namespace std;
const int INF = 2e9;
const int MOD = 1e9+;
const int MAXN = +; int N;
char a[MAXN][MAXN];
int M[MAXN][MAXN], id[MAXN][MAXN], link[MAXN];
bool vis[MAXN]; bool dfs(int u)
{
for(int i = ; i<=N; i++)
if(M[u][i] && !vis[i])
{
vis[i] = true;
if(link[i]==- || dfs(link[i]))
{
link[i] = u;
return true;
}
}
return false;
} int hungary()
{
int ret = ;
memset(link, -, sizeof(link));
for(int i = ; i<=N; i++)
{
memset(vis, , sizeof(vis));
if(dfs(i)) ret++;
}
return ret;
} int main()
{
int T, n, m;
scanf("%d", &T);
while(T--)
{
scanf("%d%d", &n, &m);
N = ;
memset(id, -, sizeof(id));
for(int i = ; i<=n; i++)
{
scanf("%s", a[i]+);
for(int j = ; j<=m; j++)
if(a[i][j]=='*')
id[i][j] = ++N;
} memset(M, false, sizeof(M));
for(int i = ; i<=n; i++)
for(int j = ; j<=m; j++)
{
if(id[i][j]==-) continue;
if(j!= && id[i][j-]!=-) M[id[i][j]][id[i][j-]] = true;
if(j!=m && id[i][j+]!=-) M[id[i][j]][id[i][j+]] = true;
if(i!= && id[i-][j]!=-) M[id[i][j]][id[i-][j]] = true;
if(i!=n && id[i+][j]!=-) M[id[i][j]][id[i+][j]] = true;
} int cnt = hungary()/;
printf("%d\n", N-cnt);
}
}

最新文章

  1. Nodejs学习笔记(一)——初识Nodejs
  2. Leetcode: Split Array Largest Sum
  3. MongoDB学习笔记-游标
  4. java问题若干
  5. [PHP]MemCached高级缓存
  6. RMAN连接及简单操作
  7. CSS自学笔记(4):CSS样式表的使用
  8. 第五节 Code 128 码
  9. 我的Python成长之路---第一天---Python基础(6)---2015年12月26日(雾霾)
  10. Python 清理HTML标签类似PHP的strip_tags函数功能(二)
  11. [转]ASP.NET Core 1 Deploy to IIS
  12. RecyclerView学习笔记
  13. 2016.02.01日,UdoOS系统项目正式开通了
  14. 前端里移动端到底比pc端多哪些知识?
  15. SGU 223 Little Kings(状压DP)
  16. springboot logback 相关使用
  17. Linux 文件查找命令详解
  18. aglio报错解决
  19. PowerDesigner安装及破解
  20. js保留两位小数方法总结

热门文章

  1. luogu1447 [NOI2010]能量采集
  2. python024 Python3 实例
  3. Android TransitionDrawable:过渡动画Drawable
  4. vue验证码点击更新
  5. msp430入门编程40
  6. json三种类型小笔记
  7. maven生命周期和依赖的范围
  8. 容器使用笔记(List篇)
  9. powerdesign导出SQL时自己主动生成凝视
  10. React笔记