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 大意:方格地图上有一些点用 '*' 表示,一个椭圆可以覆盖两个相邻的点(上下左右),问最少用多少椭圆能覆盖所有点 解法:每个 '*' 拆为两个点,能够同时覆盖的点连边,构成二分图,跑一边匹配。
要使用的椭圆数='*'总数 - 匹配数 + floor(匹配数/2)
原因:匹配数/2为 要求每个椭圆覆盖两个'*'时 能够使用的最大椭圆数。
这样覆盖后还剩下(总数 - 匹配数)个'*',对于每个剩下的'*',只能再使用一个椭圆。 提交三次
第一次选错编译器,第二次邻接表没有清空
第三次
 #include<iostream>
#include<cstdio>
#include<cstring>
#include<ctime>
#include<cstdlib>
#include<algorithm>
#include<cmath>
using namespace std;
int read(){
int xx=,ff=;char ch=getchar();
while(ch>''||ch<''){if(ch=='-')ff=-;ch=getchar();}
while(ch>=''&&ch<=''){xx=(xx<<)+(xx<<)+ch-'';ch=getchar();}
return xx*ff;
}
const int ws_[]={-,,,},ad_[]={,,,-};
int H,W,T,id[][],tx,ty,sum,ans;
char mp[][];
int lin[],len;
struct edge{
int next,y;
}e[];
inline void insert(int xx,int yy){
e[++len].next=lin[xx];
lin[xx]=len;
e[len].y=yy;
}
int vis[],tim,pretim,match[];
bool hun(int x){
for(int i=lin[x];i;i=e[i].next)
if(vis[e[i].y]<=pretim){
vis[e[i].y]=++tim;
if(match[e[i].y]==||hun(match[e[i].y])){
match[e[i].y]=x;
match[x]=e[i].y;
return ;
}
}
return ;
}
int main(){
//freopen("in","r",stdin);
//freopen("out","w",stdout);
T=read();
while(T--){
H=read(),W=read();
for(int i=;i<=H;i++){
for(int j=;j<=W;j++)
mp[i][j]=getchar(),id[i][j]=(i-)*W+j;
getchar();
}
len=;
memset(lin,,sizeof(lin));
for(int i=;i<=H;i++)
for(int j=;j<=W;j++)
if(mp[i][j]=='*')
for(int k=;k<;k++){
tx=i+ws_[k],ty=j+ad_[k];
if(tx<=||tx>H||ty<=||ty>W)
continue;
if(mp[tx][ty]=='*')
insert(id[i][j],id[tx][ty]+H*W);
}
tim=;sum=;ans=;
memset(vis,,sizeof(vis));
memset(match,,sizeof(match));
for(int i=;i<=H;i++)
for(int j=;j<=W;j++)
if(mp[i][j]=='*'){
sum++;
pretim=tim;
vis[id[i][j]]=++tim;
if(hun(id[i][j]))
ans++;
}
printf("%d\n",sum-ans+ans/);
}
return ;
}

 
 

POJ3020 二分图匹配——最小路径覆盖的更多相关文章

  1. POJ 1422 Air Raid(二分图匹配最小路径覆盖)

    POJ 1422 Air Raid 题目链接 题意:给定一个有向图,在这个图上的某些点上放伞兵,能够使伞兵能够走到图上全部的点.且每一个点仅仅被一个伞兵走一次.问至少放多少伞兵 思路:二分图的最小路径 ...

  2. UVA 1201 - Taxi Cab Scheme(二分图匹配+最小路径覆盖)

    UVA 1201 - Taxi Cab Scheme 题目链接 题意:给定一些乘客.每一个乘客须要一个出租车,有一个起始时刻,起点,终点,行走路程为曼哈顿距离,每辆出租车必须在乘客一分钟之前到达.问最 ...

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

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

  4. POJ:3020-Antenna Placement(二分图的最小路径覆盖)

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

  5. POJ 3020:Antenna Placement(无向二分图的最小路径覆盖)

    Antenna Placement Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6334   Accepted: 3125 ...

  6. hdu3861 强连通分量缩点+二分图最最小路径覆盖

    The King’s Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  7. (匹配 最小路径覆盖)Air Raid --hdu --1151

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=1151 http://acm.hust.edu.cn/vjudge/contest/view.action ...

  8. POJ-1422 Air Raid---二分图匹配&最小路径覆盖

    题目链接: https://vjudge.net/problem/POJ-1422 题目大意: 有n个点和m条有向边,现在要在点上放一些伞兵,然后伞兵沿着图走,直到不能走为止 每条边只能是一个伞兵走过 ...

  9. POJ 1422 二分图(最小路径覆盖)

    Air Raid Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 7278   Accepted: 4318 Descript ...

随机推荐

  1. Spring 定时器 定时访问数据库并发送邮件

    我这里有两个案例的方法: 第一种:使用Spring quartz: 我这里使用的jar:spring-context-support.jar.quartz-1.6.5.jar ============ ...

  2. Codeforces 833B The Bakery(主席树 + 决策单调性优化DP)

    题目链接 The Bakery 题目大意:目标是把$n$个数分成$k$组,每个组的值为这个组内不同的数的个数,求$k$个组的值的和的最大值. 题目分析: 这道题我的解法可能和大众解法不太一样……我用主 ...

  3. T1046 旅行家的预算 codevs

    http://codevs.cn/problem/1046/ 题目描述 Description 一个旅行家想驾驶汽车以最少的费用从一个城市到另一个城市(假设出发时油箱是空的).给定两个城市之间的距离D ...

  4. Maven生成项目文档

    Maven项目可以通过maven-site-plugin插件生成项目文档,无论什么项目都可以生成. 执行命令: mvn site 生成完成的输出目录在${basedir}/target/site文件夹 ...

  5. Opengl配置

    Opengl配置说明: 本配置文档针对windows64位操作系统,配置vs2008项目工程 1.下载OpenGL的glut类库:http://www.opengl.org/resources/lib ...

  6. websocket笔记

    本文为原创,转载请注明出处: cnzt       文章:cnzt-p http://www.cnblogs.com/zt-blog/p/6742746.html websocket -- 双向通信网 ...

  7. SSM框架笔记

    配置 Project结构 SpringMVC启用 Spring MVC配置 Spring自己主动扫描 getBean的方法 SpringMVC与Struts2的差别 Log4j 拦截器与过滤器 文件U ...

  8. Fragment 生命周期怎么来的?

    前言 Fragment对于 Android 开发人员来说一点都不陌生,由于差点儿不论什么一款 app 都大量使用 Fragment,所以 Fragment 的生命周期相信对于大家来说应该都非常清晰.但 ...

  9. Thinking in React(翻译)

    下面是React官方文档中的Thinking inReact文章的翻译,第一次翻译英文的文章,肯定有非常多不对的地方,还望多多包涵. 原文地址:https://facebook.github.io/r ...

  10. 《python源代码剖析》笔记 Python的编译结果

    本文为senlie原创.转载请保留此地址:http://blog.csdn.net/zhengsenlie 1.python的运行过程 1)对python源码进行编译.产生字节码 2)将编译结果交给p ...