POJ3020:Antenna Placement(二分图匹配)
Antnna Placement
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 11093 | Accepted: 5459 |
题目链接:http://poj.org/problem?id=3020
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
题意:
一根天线可以套住两个各自,问至少需要多少跟天线可以套住所有的‘o’。
题解:
与这道题有类似之处:http://acm.hdu.edu.cn/showproblem.php?pid=4185
考虑二分图匹配,将每个“o”看作一个点,然后挨着的“o”与它相连,二分图匹配的可行性在于一个“o”最多被使用一次。
这里利用贪心的思想,先求出二分图的最大匹配,然后对于没有连到的点单独用一根天线去连,最后直接输出答案即可。
代码如下:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define mem(x) memset(x,0,sizeof(x))
using namespace std; const int N = ;
int map[N][N],check[N*],match[N*],vis[N*],link[N*][N*];
int n,m,t,tot,ans,dfn;
char s[N];
inline void init(){
mem(map);mem(match);mem(link);mem(check);
mem(vis);tot=;ans=;dfn=;
}
inline void update(int x,int y){
if(map[x+][y]) link[map[x][y]][map[x+][y]]=;
if(map[x-][y]) link[map[x][y]][map[x-][y]]=;
if(map[x][y+]) link[map[x][y]][map[x][y+]]=;
if(map[x][y-]) link[map[x][y]][map[x][y-]]=;
}
inline int dfs(int x){
for(int i=;i<=tot;i++){
if(link[x][i] && check[i]!=dfn){
check[i]=dfn;
if(!match[i] || dfs(match[i])){
match[i]=x;
return ;
}
}
}
return ;
}
int main(){
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&m);
init();
for(int i=;i<=n;i++){
scanf("%s",s+);
for(int j=;j<=m;j++) if(s[j]=='*') map[i][j]=++tot;
}
for(int i=;i<=n;i++) for(int j=;j<=m;j++) if(map[i][j]) update(i,j);
for(int i=;i<=tot;i++){
dfn++;
if(dfs(i)) ans++;
}
for(int i=;i<=tot;i++) if(match[i]) vis[match[i]]=vis[i]=;
for(int i=;i<=tot;i++) if(!vis[i]) ans+=;
printf("%d\n",ans/);
}
return ;
}
POJ3020:Antenna Placement(二分图匹配)的更多相关文章
- POJ3020——Antenna Placement(二分图的最大匹配)
Antenna Placement DescriptionThe Global Aerial Research Centre has been allotted the task of buildin ...
- 【POJ 3020】Antenna Placement(二分图匹配)
相当于用1*2的板覆盖给定的h*w的格子里的点,求最少的板.可以把格子相邻的分成两个集合,如下图,0为一个集合,1的为一个,也就是(行数+列数)为奇数的是一个集合,为偶数的为另一个集合.1010101 ...
- POJ3020 Antenna Placement(二分图最小路径覆盖)
The Global Aerial Research Centre has been allotted the task of building the fifth generation of mob ...
- poj3020 Antenna Placement 匈牙利算法求最小覆盖=最大匹配数(自身对应自身情况下要对半) 小圈圈圈点
/** 题目:poj3020 Antenna Placement 链接:http://poj.org/problem?id=3020 题意: 给一个由'*'或者'o'组成的n*m大小的图,你可以用一个 ...
- POJ3020 Antenna Placement —— 最大匹配 or 最小边覆盖
题目链接:https://vjudge.net/problem/POJ-3020 Antenna Placement Time Limit: 1000MS Memory Limit: 65536K ...
- POJ3020 Antenna Placement
Antenna Placement Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9586 Accepted: 4736 ...
- POJ-3020 Antenna Placement---二分图匹配&最小路径覆盖&建图
题目链接: https://vjudge.net/problem/POJ-3020 题目大意: 一个n*m的方阵 一个雷达可覆盖两个*,一个*可与四周的一个*被覆盖,一个*可被多个雷达覆盖问至少需要多 ...
- POJ 3020 Antenna Placement (二分图最小路径覆盖)
<题目链接> 题目大意:一个矩形中,有N个城市’*’,现在这n个城市都要覆盖无线,每放置一个基站,至多可以覆盖相邻的两个城市.问至少放置多少个基站才能使得所有的城市都覆盖无线? 解题分析: ...
- POJ - 3020 Antenna Placement 二分图最大匹配
http://poj.org/problem?id=3020 首先注意到,答案的最大值是'*'的个数,也就是相当于我每用一次那个技能,我只套一个'*',是等价的. 所以,每结合一对**,则可以减少一次 ...
- Antenna Placement(二分图的最大匹配)
http://poj.org/problem?id=3020 题意: 一个矩形中,有N个城市'*',现在这n个城市都要覆盖无线,若放置一个基站,它至多可以覆盖相邻的两个城市.问至少放置多少个基站才能使 ...
随机推荐
- android 界面切换教程
Android的界面切换其实并没有什么难度,就和java的界面切换一样简单,但是java的使用的是Swing框架进行的切换,而Android使用的是Intent 来切换框架的,这是一个全新的使用方法, ...
- python2.7练习小例子(六)
6):题目:斐波那契数列. 程序分析:斐波那契数列(Fibonacci sequence),又称黄金分割数列,指的是这样一个数列:0.1.1.2.3.5.8.13.21.34.……. ...
- linux io 学习笔记(02)---条件变量,管道,信号
条件变量的工作原理:对当前不访问共享资源的任务,直接执行睡眠处理,如果此时需要某个任务访问资源,直接将该任务唤醒.条件变量类似异步通信,操作的核心:睡眠.唤醒. 1.pthread_cond_t 定 ...
- Java8新特性(一)——Lambda表达式与函数式接口
一.Java8新特性概述 1.Lambda 表达式 2. 函数式接口 3. 方法引用与构造器引用 4. Stream API 5. 接口中的默认方法与静态方法 6. 新时间日期 API 7. 其他新特 ...
- WPF把CheckBox的文字放到左边,开关在右边
原文:WPF把CheckBox的文字放到左边,开关在右边 效果 实现 这篇文章给了一个不错的参考方案. http://www.codeproject.com/Articles/19141/WPF-Ch ...
- Linq中dbSet 的查询
1.Find:按照关键字的ID号来查询(速度快) 如: ADShiTi aDShiTi = db.ADShiTis.Find(id); 2.FirstOrDefault:根据部分条件查询,显示最前的一 ...
- argos3-simulator
如何修改控制器: CVector2: class CVector2 { friend class CRotationMatrix2; friend class CTransformationMatri ...
- 可以随着SeekBar滑块滑动显示的Demo
//关于Seek的自定义样式,之前也有总结过,但是,一直做不出随着滑块移动的效果,查询了很多资料终于解决了这个问题,现在把代码写出来有bug的地方 希望大家批评指正. Step 1 :自定义一个Vie ...
- 学习bash——数据流重定向
一.概述 1. 数据流 定义:以规定顺序被读取一次的数据序列. 分类:标准输入(stdin).标准输出(stdout)和标准错误输出(stderr). 标准输出:指的是命令执行所回传的正确信息. 标准 ...
- 第18讲——string类
关键字:string类 字符串 C-风格字符串 C库字符串函数 字符串:存储在内存的连续字节中的一系列字符. C++处理字符串的方式有两种: 来自C语言,常被称为C-风格字符串: 基于strin ...