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个城市都要覆盖无线,若放置一个基站,它至多可以覆盖相邻的两个城市.问至少放置多少个基站才能使 ...
随机推荐
- rails中如何在a标签中添加其他标签
最近在用rails写一个项目练练手,然后遇到了一个问题,就是用 <% link_to("首页", root_path) %> 生成一个a标签,之后就在想我怎么在这个a标 ...
- 第一个网页(仿照当当网,仅仅使用CSS)
这个网页是在学过CSS之后,对当当网首页进行模仿的网页,没有看当当网的网页源码,纯按照自己之前学的写的,由于是刚学过HTML和CSS才一个星期,所以里面有许多地方写的非常没有水平,仅仅用来学习使用,欢 ...
- office 总结
wps word中双击格式刷即可开启永久格式刷
- TCD产品技术参考资料
1.Willis环 https://en.wikipedia.org/wiki/Circle_of_Willis 2.TCD仿真软件 http://www.transcranial.com/index ...
- Android——搜索传统蓝牙设备
一,主布局: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:andro ...
- A problem occurred evaluating project ':'. > ASCII
项目编译出错: 错误信息如下: FAILURE: Build failed with an exception. * Where: Build file 'F:\git\i***\build.grad ...
- Hystrix入门指南
Introduction 1.Where does the name come from? hystrix对应的中文名字是“豪猪”,豪猪周身长满了刺,能保护自己不受天敌的伤害,代表了一种防御机制,这与 ...
- Android系统自带样式
android:theme="@android:style/Theme.Dialog" 将一个Activity显示为能话框模式 android:theme="@andr ...
- python 基础篇 12 装饰器进阶
本节主要内容:1. 通⽤装饰器回顾2. 函数的有⽤信息3. 带参数的装饰器4. 多个装饰器同时装饰⼀个函数 ⼀. 通⽤装饰器的回顾开闭原则: 对增加功能开放. 对修改代码封闭装饰器的作⽤: 在不改变原 ...
- windows本地连接腾讯云的mysql服务器
由于最近数据库需要用上Navicat作为数据库,但是我的mysql装在腾讯云的Ubuntu上,因此需要做些配置开放端口,和监听端口,因此略显麻烦,这里记录一下连接的具体步骤,方便以后又得装(flag) ...