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. eclipse中创建maven web项目

    本文主要说明将maven web项目转成eclipse支持的web项目. 创建一个maven项目设置打包类型为war则其为web项目 结构如下 将mavenweb项目转成eclipse识别的web项目 ...

  2. 记录一次手机联系人整理(XML文件格式处理)

    场景:1.IOS手机和Android手机联系人同步时有部分重复联系人. 2.很早以前的HTC手机导出的联系人中备注信息有大量乱码,且很多联系人生日被设置为1970-01-01,导致生日提醒软件产生骚扰 ...

  3. Android ListView 设置

    android:minHeight="80dip"//设置每一条的高度 android:divider="@null" //设置默认的分割线不显示 androi ...

  4. import random随机生成验证码

    #!/usr/bin/env python import random temp = "" for i in range(6): num = random.randrange(0, ...

  5. IDEA创建Maven Web 项目

    前提:安装过maven并且配置了maven的环境变量,这里就不演示了.转载了别人一篇maven详解,不了解的可以先看一下这个 链接 图文讲解: 创建项目 选择Maven 选择创建webapp项目 指定 ...

  6. 如何设置Win10文件资源管理器默认打开“这台电脑”

    摘录自:http://www.ithome.com/html/win10/126066.htm

  7. C#简单的图片合成及防止并发的办法

    /// <summary> /// 合成图 /// </summary> private string ComposeCarBrandBadImage(AnonAttachme ...

  8. Windows系统 为 Visual Studio软件 搭建 OpenCV2 开发环境

    Windows系统 为 Visual Studio软件 搭建 OpenCV2 开发环境 我们的电脑系统:Windows 10 64位 Visual Studio 软件:Visual Studio 20 ...

  9. Entity Framework Tutorial Basics(3):Entity Framework Architecture

    Entity Framework Architecture The following figure shows the overall architecture of the Entity Fram ...

  10. 多线程学习-基础( 十)一个synchronized(){/*代码块*/}简单案例分析

    一.提出疑惑 上一篇文章中,分析了synchronized关键字的用法.但是好像遗漏了一种情况. 那就是: synchronized(obj){/*同步块代码*/} 一般有以下几种情况: (1)syn ...