Antenna Placement poj 3020
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 12104 | Accepted: 5954 |
Description
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
Output
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
Source
// 题意:给一张图,图中有两种点,一次只能覆盖相邻(上下左右)两点
// 求最小覆盖数
// 二分图最小覆盖数等于最大匹配数,匈牙利算法求最大匹配数
// 此题建图方面有些繁琐,看了大佬的题解后,才建图成功 #include <cstdio>
#include <cstring> using namespace std; const int max_h = ;
const int max_w = ; int h,w;
char s[max_h][max_w]; int n;
int total=,save=,cur;
int direct[][]={{,},{-,},{,},{,-}}; const int max_n=+;
int cx[max_n],cy[max_n],st[max_n];
bool vis[max_n]; struct node
{
int y,nxt;
};
// 这里数组开的小了点,但是刚好够用了,大佬在题解中开了比这大百倍的数组。不知道为什么
// 这个数组的最大用量,应该由add函数的使用上限决定
// 每遍历一个点,最多在上下左右四个方向进行一次add函数的调用,所以只需要4*max_n次即可
node way[]; // 计算在一维数组中的位置
int get(int i,int j)
{
return i*w+j;
} void add(int u,int v)
{
++cur;
// 当前数组中存储邻接点v和st【u】
way[cur].y=v;
way[cur].nxt=st[u];
// st数组存储当u的边在数组中的位置
st[u]=cur;
} int match(int x)
{
for(int i=st[x];i;i=way[i].nxt)
{
int y=way[i].y;
if(!vis[y])
{
vis[y]=;
if(!cy[y] || match(cy[y]))
{
cx[x]=y;
cy[y]=x;
return ;
}
}
}
return ;
} int XYL()
{
memset(cx,,sizeof(cx));
memset(cy,,sizeof(cy));
int ans=;
for(int i=;i<n;++i)
{
// 遍历所有节点,如果找到没有匹配的x,看能否找到与之匹配的y
// 这里加了一步判断,对st[i]不为0的判断,也就是对当前节点不为'o'的判断
// 不加这一个判断也可得出正确的结果,但加入后会有优化,不用再执行之后许多无用的操作
// 毕竟只有'*'的点需要匹配不是吗?
if(!cx[i] && st[i])
{
memset(vis,,sizeof(vis));
ans+=match(i);
}
}
return ans;
} int main()
{
int T;
scanf("%d",&T);
while(T--)
{
// 输入数据
scanf("%d %d",&h,&w);
for(int i=;i<h;++i)
{
scanf("%s",s[i]);
} // 初始化way数组和st数组
cur=;
memset(st,,sizeof(st));
// n表示总节点数
n=h*w;
// total表示*的总数
total=; for(int i=;i<h;++i)
{
for(int j=;j<w;++j)
{
if(s[i][j]=='*')
{
++ total;
// 检查相邻四个方向
for(int k=;k<;++k)
{
int tx=i+direct[k][];
int ty=j+direct[k][];
// 在图的范围内且为*时,加边
if(tx>= && tx<h && ty>= && ty<w)
{
if(s[tx][ty]=='*')
{
int u=get(i,j);
int v=get(tx,ty);
add(u,v);
// printf("add");
}
}
}
}
}
} //printf("cur:%d\n",tot); printf("%d\n",total-XYL()/);
}
return ;
} /*
2
7 9
ooo**oooo
**oo*ooo*
o*oo**o**
ooooooooo
*******oo
o*o*oo*oo
*******oo
10 1
*
*
*
o
*
*
*
*
*
*
*/
Antenna Placement poj 3020的更多相关文章
- Antenna Placement POJ - 3020 (最小边集覆盖)
Antenna Placement Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10699 Accepted: 526 ...
- Antenna Placement poj 3020(匹配)
http://poj.org/problem?id=3020 题意:给定一个n*m的矩阵,'*'代表城市,现在想要用1*2的矩阵将所有的城市覆盖,问最少需要多少个矩阵? 分析:先为每个城市进行标号,再 ...
- (匹配 二维建图) Antenna Placement --POJ --3020
链接: http://poj.org/problem?id=3020 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82834#probl ...
- (匹配)Antenna Placement --POJ --3020
链接: http://poj.org/problem?id=3020 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82834#probl ...
- Antenna Placement POJ - 3020 二分图匹配 匈牙利 拆点建图 最小路径覆盖
题意:图没什么用 给出一个地图 地图上有 点 一次可以覆盖2个连续 的点( 左右 或者 上下表示连续)问最少几条边可以使得每个点都被覆盖 最小路径覆盖 最小路径覆盖=|G|-最大匹配数 ...
- poj 3020 Antenna Placement(最小路径覆盖 + 构图)
http://poj.org/problem?id=3020 Antenna Placement Time Limit: 1000MS Memory Limit: 65536K Total Sub ...
- POJ 3020 Antenna Placement【二分匹配——最小路径覆盖】
链接: http://poj.org/problem?id=3020 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...
- POJ 3020 Antenna Placement 【最小边覆盖】
传送门:http://poj.org/problem?id=3020 Antenna Placement Time Limit: 1000MS Memory Limit: 65536K Total ...
- POJ 3020——Antenna Placement——————【 最小路径覆盖、奇偶性建图】
Antenna Placement Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u S ...
随机推荐
- Grevl旅游注册的初步界面,以源代码和运行图片展示
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- 视觉slam十四讲第8章课后习题3+稀疏直接法程序注释
版权声明:本文为博主原创文章,转载请注明出处: http://www.cnblogs.com/newneul/p/8571653.html 3.题目回顾:在稀疏直接法中,假设单个像素周围小块的光度也不 ...
- PTA 7-9 集合相似度(STL之set初体验)
7-9 集合相似度(25 分) 给定两个整数集合,它们的相似度定义为:Nc/Nt×100%.其中Nc是两个集合都有的不相等整数的个数,Nt是两个集合一共有的不相等整数的个数.你 ...
- Kafka运维大全来了!优化、监控、故障处理……
Kafka概念 Kafka是分布式发布-订阅消息系统.它最初由LinkedIn公司开发,之后成为Apache项目的一部分.Kafka是一个分布式的.可划分的.冗余备份的.持久性的日志服务.它主 ...
- LDAP安装
一.介绍 LDAP 全称:Lightweight Directory Access Protocol,即“轻量级目录访问协议”. LDAP目录以树状的层次结构来存储数据.如果你对自顶向下的DNS树或U ...
- mybatis 通过配置父类数据源连接和关闭数据,进行junit单元测试
来源:https://blog.csdn.net/Bigbig_lyx/article/details/80646005 解决问题,单元测试没经过单独配置,每个测试方法中要添加配置数据源 一:配置父类 ...
- Android5.0和Android6.0适配
gradle配置项 compileSdkVersion 用哪个 Android SDK 版本编译你的应用.因此我们强烈推荐总是使用最新的 SDK 进行编译.在现有代码上使用新的编译检查可以获得很多好处 ...
- ORACLE ANALYZE使用小结
ANALYZE的介绍 使用ANALYZE可以收集或删除对象的统计信息.验证对象的结构.标识表或cluster中的行迁移/行链接信息等.官方文档关于ANALYZE功能介绍如下: · ...
- 《ADCrowdNet》密集人群检测论文笔记
背景 为了解决高密度的计数问题.(PS:就是attention机制的应用) 网络 整体网络结构图 attention部分的网络AMG 密度图预测网络 DConv代表可变形卷积,下图是常规卷积(左)与可 ...
- mysql 主主备份
1.1.主主备份原理. 主主备份实际上是互为主从,主要是为了去缓解写入压力. 1.2.环境准备 两台机器ip分别为 100.100.100.105 (主1) 100.100.100.106(主2) 安 ...