Antenna Placement
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 10518   Accepted: 5189

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

题目链接:点击打开链接

题目大意:给你一幅图,若干个点,然后有一种信号塔可以向图片上那样上左下右的覆盖,问你最少要建几个信号塔。

思路:这道题和poj3041有一点点像吧(关于3041可以看我另外一篇博客)。只不过那道题是求最小覆盖点,这道题是求最小覆盖边。那怎么想呢,首先,贪心,构造会很麻烦,而且应该是不能成功的(因为给你的图的边不一样,点不一样)而这道题是要我们用若干个东西覆盖若干个东西对不对,这就想到了匈牙利算法里面的两个覆盖。

先提两个定理:最小覆盖点集数=最大匹配数,最小覆盖边集数=顶点数-最大匹配数。

此题我们只用到了后者。什么是最小覆盖边集呢,我们知道二分图是用左右两个点集,加中间的边组成的,最小覆盖边集的意思就是,二分图任意一个顶点,都有一条边处于这个集合中,而这个定理的正确性我不予证明(其实是不会)。

这道题我们仔细思考一下,会发现,任意一个点,只能和周围的四个点公用一个信号塔,再想一下,中间的点和周围的点横纵坐标之和奇偶性是刚好相反的,再想一下!整幅图都只有两种点,那就是横纵左边之和为奇数或者为偶数的点,那二分图的点集就建好了,左边是奇数点,右边是偶数点,易得两边都没有自交边,只能和对方相连,那二分图的边是什么呢,显然就是,如果一个奇数点和偶数点相邻,那就是有一条边(也就是两个点可以共用一个信号塔),这样建完了图后,那怎么做就一目了然了,就是求最少的边将点全部覆盖,也就是求最小覆盖边集了。

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<queue>
#include<iostream>
#include<algorithm>
#define MAXN 10100
#define INF 0x7fffffff
#define max(a,b) a>b?a:b
#define min(a,b) a>b?b:a
using namespace std;
const int maxn=60;
char mp[maxn][maxn];
int g[1000][1000],used[1000],girl[1000];
struct dian{
int x,y;
}a[1000],b[1000];//统计奇数点和偶数点
int aa,bb;
bool find(int x){//匈牙利算法
for(int i=1;i<=bb;i++){
if(g[x][i]&&used[i]==0){
used[i]=1;
if(girl[i]==0||find(girl[i])){
girl[i]=x;
return true;
}
}
}
return false;
}
int main(){
int t;
cin>>t;
while(t--){
memset(g,0,sizeof(g));
memset(girl,0,sizeof(girl));
int h,w;
scanf("%d%d",&h,&w);
for(int i=0;i<h;i++){
scanf("%s",mp[i]);
}
aa=0,bb=0;
for(int i=0;i<h;i++){
for(int j=0;j<w;j++){
if(mp[i][j]=='*'){//
if((i+j)&1){//统计奇数点和偶数点
a[++aa].x=i;
a[aa].y=j;
}else{
b[++bb].x=i;
b[bb].y=j;
}
}
}
}
for(int i=1;i<=aa;i++){//对于每个奇数点 如果偶数点和他相邻 则存在一条边
for(int j=1;j<=bb;j++){
if(a[i].x==b[j].x){
if(abs(a[i].y-b[j].y)<=1){
g[i][j]=1;
}
}
if(a[i].y==b[j].y){
if(abs(a[i].x-b[j].x)<=1){
g[i][j]=1;
}
}
}
}
int ans=0;
for(int i=1;i<=aa;i++){
memset(used,0,sizeof(used));
if(find(i))ans++;
}
printf("%d\n",aa+bb-ans);//最小覆盖边集数=顶点数-最大匹配数
}
}

poj3020 建信号塔(匈牙利算法 最小覆盖边集)的更多相关文章

  1. POJ3041轰炸行星(匈牙利算法 最小覆盖点集)

    Asteroids Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 25232   Accepted: 13625 Descr ...

  2. Machine Schedule(二分图匹配之最小覆盖点,匈牙利算法)

    个人心得:二分图啥的一点都不知道,上网借鉴了下,请参考http://blog.csdn.net/thundermrbird/article/details/52231639 加上自己的了解,二分图就是 ...

  3. HDU 1150 Machine Schedule (最小覆盖,匈牙利算法)

    题意: 有两台不同机器A和B,他们分别拥有各种运行模式1~n和1~m.现有一些job,需要在某模式下才能完成,job1在A和B上需要的工作模式又可能会不一样.两台机器一开始处于0模式,可以切换模式,但 ...

  4. [ACM_图论] The Perfect Stall 完美的牛栏(匈牙利算法、最大二分匹配)

    描述 农夫约翰上个星期刚刚建好了他的新牛棚,他使用了最新的挤奶技术.不幸的是,由于工程问题,每个牛栏都不一样.第一个星期,农夫约翰随便地让奶牛们进入牛栏,但是问题很快地显露出来:每头奶牛都只愿意在她们 ...

  5. UVA 11419 SAM I AM (最小点覆盖,匈牙利算法)

    题意:给一个r*c的矩阵,某些格子中可能有一些怪物,可以在一行或一列防止一枚大炮,大炮会扫光整行/列的怪,问最少需要多少炮?输出炮的位置. 思路: 先每行和列都放一个炮,把炮当成点,把怪当成边,一边连 ...

  6. 【入门】匈牙利算法+HNOI2006 hero超级英雄

    一.关于匈牙利算法 匈牙利算法是由匈牙利数学家Edmonds提出的,用增广路径求二分图最大匹配的算法. 听起来高端,其实说白了就是: 假设不存在单相思(单身狗偷偷抹眼泪),在一个同性恋不合法的国家里( ...

  7. HDU 5943 Kingdom of Obsession 【二分图匹配 匈牙利算法】 (2016年中国大学生程序设计竞赛(杭州))

    Kingdom of Obsession Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Oth ...

  8. UVALive5874 - Social Holidaying-二分图匹配/匈牙利算法

    有n个家庭,m个房间,一个房间只能两个家庭住.求最大匹配. 比较标准的二分图问题.先初始化把可能的家庭建边,然后跑一边匈牙利算法. 最后的答案是最大匹配数/2,因为建图时有重复. #include & ...

  9. 二分图最大匹配|UOJ#78|匈牙利算法|边表|Elena

    #78. 二分图最大匹配 从前一个和谐的班级,有 nlnl 个是男生,有 nrnr 个是女生.编号分别为 1,…,nl1,…,nl 和 1,…,nr1,…,nr. 有若干个这样的条件:第 vv 个男生 ...

随机推荐

  1. spring各版本下载地址

    发现从spring.io里面找下载连接不好找了,但是机智的我还是找到了,这里做下分享 一.纯spring http://repo.spring.io/release/org/springframewo ...

  2. 第5章 使用MUI与H5+构建移动端app

    H5+是JS封装的工具集合,通过H5+我们就可以使用JS的方式去调用到我们手机端上的一些原生的组件. http://dev.dcloud.net.cn/mui/ http://dev.dcloud.n ...

  3. Codeforces #495 Div2 problem E. Sonya and Ice Cream(1004E)

    网上的大多是用树的直径做的,但是一些比较巧妙的做法,来自https://www.cnblogs.com/qldabiaoge/p/9315722.html. 首先用set数组维护每一个节点所连接的边的 ...

  4. (转载)Eclipse报错:java.lang.ClassNotFoundException: ContextLoaderListener

    转载自:http://www.cnblogs.com/love540376/p/5527757.html Eclipse中tomcat部署工程启动后报错: 严重: Error configuring  ...

  5. strcmp()比较函数和strcasecmp()和strnatcmp()

    strcmp()的函数原型如下() int strcmp(string str1,string str2) 该函数需要两个进行比较的参数字符串,如果这两个字符串相等,该函数就返回0,如果按字典顺序st ...

  6. git在eclipse中的配置 完整版 转载

    http://www.cnblogs.com/zhxiaomiao/archive/2013/05/16/3081148.html

  7. 面试题:struts 值栈 有用

    一. 核心部分 1. [核心试题]完成当天课堂练习 2. [多选题] 阅读如下代码中,下列哪种方式可以在页面正确迭代获取集合中的数据 (ABC) public String add(){ ValueS ...

  8. Centos7安装mysql缺乏yum源怎么安装

    找到mysql5.6的centos的repo源,终于解决mysql的安装问题: 1.确保centos安装了wget,没有的话安装wget   1 yum install wget 2.下载mysql的 ...

  9. MSER

    1.注释很全的分析:http://blog.csdn.net/zhaocj/article/details/40742191 2.opencv采用的mser实现方法 * 1. the gray ima ...

  10. beforeFilter()

    在控制器每个动作之前执行,可以方便地检查有效的会话,或者检查用户的权限. function beforeFilter() { parent::beforeFilter(); if(empty($thi ...