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. 【Android】状态栏通知Notification、NotificationManager详解(转)

    在Android系统中,发一个状态栏通知还是很方便的.下面我们就来看一下,怎么发送状态栏通知,状态栏通知又有哪些参数可以设置? 首先,发送一个状态栏通知必须用到两个类:  NotificationMa ...

  2. codevs——1503 愚蠢的宠物

    1503 愚蠢的宠物  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解  查看运行结果     题目描述 Description 大家都知道,sheep有两 ...

  3. hdu 4975 A simple Gaussian elimination problem 最大流+找环

    原题链接 http://acm.hdu.edu.cn/showproblem.php?pid=4975 这是一道很裸的最大流,将每个点(i,j)看作是从Ri向Cj的一条容量为9的边,从源点除法连接每个 ...

  4. SPOJ 26108 TRENDGCD - Trending GCD

    Discription Problem statement is simple. Given A and B you need to calculate S(A,B) . Here, f(n)=n, ...

  5. SystemTap 静态探针安装包

     yum install systemtap-sdt-devel 

  6. java内部类理解使用

    这是我学习Java内部类的笔记 1.为什么使用内部类?使用内部类最吸引人的原因是:每个内部类都能独立地继承一个(接口的)实现,所以无论外围类是否已经继承了某个(接口的)实现,对于内部类都没有影响1.1 ...

  7. Invalid regular expression: unmatched parentheses

    Unmatched ) in Javascript regular expression您的某些字符串包含错误')'.你需要逃避这个.这是这样做的功能: function escapeRegExp(s ...

  8. 使用webstorm+webpack构建简单入门级“HelloWorld”的应用&&构建使用jquery来实现

    使用webstorm+webpack构建简单入门级“HelloWorld”的应用&&构建使用jquery来实现 1.首先你自己把webstorm安装完成. 请参考这篇文章进行安装和破解 ...

  9. 基于Android的远程视频监控系统(含源码)

    基本过程是android作为socket客户端将采集到的每一帧图像数据发送出去,PC作为服务器接收并显示每一帧图像实现远程监控.图片如下(后来PC端加了个拍照功能)... (PS.刚学android和 ...

  10. Android实现SQLite数据库的增、删、改、查的操作

    核心代码DAO类 package com.examp.use_SQLite.dao; import java.util.ArrayList; import java.util.List; import ...