Antenna Placement

Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

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 题目大意:用1*2的长条覆盖图中的“*”。问最少需要多少个长条。 解题思路:最小路径覆盖:选择最少的边,让每个顶点都被选中,单独的结点可以作为一条路径。建图思路都是奇偶性建图。但是不同的是,这个需要把所有“*”都覆盖。那么我们考虑最小路径覆盖。
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<queue>
#include<vector>
#include<algorithm>
using namespace std;
const int maxn = 660;
const int INF = 0x3f3f3f3f;
vector<int>G[maxn];
int Mx[maxn], My[maxn], dx[maxn], dy[maxn], used[maxn], dis;
char Map[maxn][maxn];
int lis[maxn][maxn];
bool SearchP(int _n){
queue<int>Q;
memset(dx,-1,sizeof(dx));
memset(dy,-1,sizeof(dy));
int dis = INF;
for(int i = 1; i <= _n; i++){
if(Mx[i] == -1){
dx[i] = 0;
Q.push(i);
}
}
int v;
while(!Q.empty()){
int u = Q.front(); Q.pop();
if(dx[u] > dis) break;
for(int i = 0; i < G[u].size(); i++){
v = G[u][i];
if(dy[v] == -1){
dy[v] = dx[u] + 1;
if(My[v] == -1){
dis = dy[v];
}else{
dx[My[v]] = dy[v] + 1;
Q.push(My[v]);
}
}
}
}
return dis != INF;
}
int dfs(int u){
int v;
for(int i = 0; i < G[u].size(); i++){
v = G[u][i];
if(!used[v] && dy[v] == dx[u] + 1){
used[v] = 1;
if(My[v] != -1 && dy[v] == dis){
continue;
}
if(My[v] == -1 || dfs(My[v])){
Mx[u] = v;
My[v] = u;
return true;
}
}
}
return false;
}
int MaxMatch(int ln,int rn){
int ret = 0;
memset(Mx,-1,sizeof(Mx));
memset(My,-1,sizeof(My));
while(SearchP(ln)){
memset(used,0,sizeof(used));
for(int i = 1; i <= ln; i++){
if(Mx[i] == -1 && dfs(i)){
ret++;
}
}
}
return ret;
}
int main(){
int T, cas = 0, n, m, N, M;
scanf("%d",&T);
while(T--){
n = m = 0;
scanf("%d%d",&N,&M);
for(int i = 0; i <= 210; i++){
G[i].clear();
}
for(int i = 0; i <= M+1; i++){
Map[N+1][i] = 'o';
Map[0][i] = 'o';
}
for(int i = 0; i <= N+1; i++){
Map[i][0] = 'o';
Map[i][M+1] = 'o';
}
for(int i = 1; i <= N; i++){
getchar();
for(int j = 1; j <= M; j++){
scanf("%c",&Map[i][j]);
if(Map[i][j] == '*'){
if((i+j)%2 == 0){
++n;
lis[i][j] = n;
if(Map[i-1][j] == '*'){
G[n].push_back(lis[i-1][j]);
}
if(Map[i][j-1] == '*'){
G[n].push_back(lis[i][j-1]);
}
}else{
++m;
lis[i][j] = m;
if(Map[i-1][j] == '*'){
G[lis[i-1][j]].push_back(m);
}
if(Map[i][j-1] == '*'){
G[lis[i][j-1]].push_back(m);
}
}
}
}
}
int res = MaxMatch(n,m);
printf("%d\n",n+m-res);
}
return 0;
}
/*
55
4 4
o***
oo*o
oooo
oooo */

  


POJ 3020——Antenna Placement——————【 最小路径覆盖、奇偶性建图】的更多相关文章

  1. poj 3020 Antenna Placement (最小路径覆盖)

    链接:poj 3020 题意:一个矩形中,有n个城市'*'.'o'表示空地,如今这n个城市都要覆盖无线,若放置一个基站, 那么它至多能够覆盖本身和相邻的一个城市,求至少放置多少个基站才干使得全部的城市 ...

  2. 二分图最大匹配(匈牙利算法) POJ 3020 Antenna Placement

    题目传送门 /* 题意:*的点占据后能顺带占据四个方向的一个*,问最少要占据多少个 匈牙利算法:按坐标奇偶性把*分为两个集合,那么除了匹配的其中一方是顺带占据外,其他都要占据 */ #include ...

  3. poj 3020 Antenna Placement(最小路径覆盖 + 构图)

    http://poj.org/problem?id=3020 Antenna Placement Time Limit: 1000MS   Memory Limit: 65536K Total Sub ...

  4. POJ 3020 Antenna Placement【二分匹配——最小路径覆盖】

    链接: http://poj.org/problem?id=3020 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...

  5. poj 3020 Antenna Placement (最小路径覆盖)

    二分图题目 当时看到网上有人的博客写着最小边覆盖,也有人写最小路径覆盖,我就有点方了,斌哥(kuangbin)的博客上只给了代码,没有解释,但是现在我还是明白了,这是个最小路径覆盖(因为我现在还不知道 ...

  6. POJ 3020 Antenna Placement (二分图最小路径覆盖)

    <题目链接> 题目大意:一个矩形中,有N个城市’*’,现在这n个城市都要覆盖无线,每放置一个基站,至多可以覆盖相邻的两个城市.问至少放置多少个基站才能使得所有的城市都覆盖无线? 解题分析: ...

  7. POJ 3020 Antenna Placement 【最小边覆盖】

    传送门:http://poj.org/problem?id=3020 Antenna Placement Time Limit: 1000MS   Memory Limit: 65536K Total ...

  8. POJ - 3020 Antenna Placement(最小覆盖路径)

    ---恢复内容开始--- https://vjudge.net/problem/POJ-3020 题意 *--代表城市,o--代表空地 给城市安装无线网,一个无线网最多可以覆盖两座城市,问覆盖所有城市 ...

  9. POJ 3020 Antenna Placement 最大匹配

    Antenna Placement Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6445   Accepted: 3182 ...

随机推荐

  1. github基本命令

    https://github.com/explore 使用github网站搜索资源 具体搜索 Git教程 - 廖雪峰的官方网站 一. git配置命令 1.设置用户名 git config --glob ...

  2. SQLServer数据库,表内存,实例名分析SQL语句

    --数据库内存分析 USE master go DECLARE @insSize TABLE(dbName sysname,checkTime VARCHAR(19),dbSize VARCHAR(5 ...

  3. java 获取前几天时间

    java   获取前几天时间 一.toLocaleString()函数过时: //当前时间   Date endDate=new Date();   String endTime=endDate.to ...

  4. Luogu2114 [NOI2014]起床困难综合症 【位运算】

    题目分析: 按位处理即可 代码: #include<bits/stdc++.h> using namespace std; ; int n,m; int a[maxn],b[maxn]; ...

  5. Spark大数据处理 之 RDD粗粒度转换的威力

    在从WordCount看Spark大数据处理的核心机制(2)中我们看到Spark为了支持迭代和交互式数据挖掘,而明确提出了内存中可重用的数据集RDD.RDD的只读特性,再加上粗粒度转换操作形成的Lin ...

  6. Halum UVA - 11478 差分约束

    输入输出格式 输入格式: 输出格式: 输入输出样例 输入样例#1: 复制 2 1 1 2 10 2 1 1 2 -10 3 3 1 2 4 2 3 2 3 1 5 4 5 2 3 4 4 2 5 3 ...

  7. zabbix+telegram的API接口(告警)

    首先在telegram里创建一个有API接口的用户,创建是在   @BotFather 选择/start——————/newbot 输入机器人的用户名,根据提示操作.获得bot的API接口和群ID 通 ...

  8. 【2014年百度之星资格赛1001】Energy Conversion

    Problem Description 魔法师百小度也有遇到难题的时候—— 现在,百小度正在一个古老的石门面前,石门上有一段古老的魔法文字,读懂这种魔法文字需要耗费大量的能量和大量的脑力. 过了许久, ...

  9. Dell N5010 Win7下 玩CS全屏(1366*768)问题

    在Dell N5010 Win7 下完CS1.6 全屏不了,以下是解决办法 1.按windows+r键打开 运行 窗口,输入regedit,打开注册表 2.依次点开 HKEY_LOCAL_MACHIN ...

  10. Educational Codeforces Round 11 B

    Description Consider 2n rows of the seats in a bus. n rows of the seats on the left and n rows of th ...