poj 3020 Antenna Placement(最小路径覆盖 + 构图)
http://poj.org/problem?id=3020
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 7565 | Accepted: 3758 |
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
详细请参考:
http://blog.csdn.net/ripwangh/article/details/47334941
题目大意:
给定一个地图,*代表城市,o代表空地,用天线来覆盖相邻的两个城市,问最少需要多少天线?(所谓相邻是指上下左右4个方向相邻)
最小路径覆盖问题,难点就是如何构图
假如是这个例子:
2
3 3
*oo
***
o*o
统计城市数量,并将其编号:
2
3 3
1oo
234
o5o
第一个城市编号1,天线可覆盖与它相邻的城市2,则1与2之间有一条连线也就是1与2匹配即G[1][2]= 1;
第二个城市编号2,天线可覆盖与它相邻的城市1,3;则G[2][1]=1,G[2][3]=1;
同理可得G[3][2]=1,G[3][4]=1,G[3][5]=1,G[4][3]=1,G[5][3]=1
将城市拆分成两部分分别存入集合X和集合Y中
X Y
1 1
2 2
3 3
4 4
5 5
构图后为:
| X | 1 | 2 | 3 | 4 | 5 | |
| Y | ||||||
| 1 | 0 | 1 | 0 | 0 | 0 | |
| 2 | 1 | 0 | 1 | 0 | 0 | |
| 3 | 0 | 1 | 0 | 1 | 1 | |
| 4 | 0 | 0 | 1 | 0 | 0 | |
| 5 | 0 | 0 | 1 | 0 | 0 |
最小路径覆盖 = 总点数 - 最大匹配数
由于将城市拆分成两份,所以求得的最大匹配应该除以2
#include<stdio.h>
#include<queue>
#include<string.h>
#include<algorithm>
#define INF 0x3f3f3f3f
#define N 1010
using namespace std; int G[N][N], A[N][N], vis[N], used[N];
char maps[N][N];
int m, n, k, d[][] = {{, }, {, -}, {, }, {-, }}; bool Find(int u)//求最大匹配
{
int i;
for(i = ; i <= k ; i++)
{
if(!vis[i] && G[u][i])
{
vis[i] = ;
if(!used[i] || Find(used[i]))
{
used[i] = u;
return true;
}
}
}
return false;
} void Build(int x, int y)//构图
{
int a, b, i;
for(i = ; i < ; i++)//无线会覆盖东南西北四个方向
{
a = x + d[i][];
b = y + d[i][];
if(a >= && a <= m && b >= && b <= n)
{
G[A[x][y]][A[a][b]] = ;
G[A[a][b]][A[x][y]] = ;
}
}
} int main()
{
int t, i, j;
scanf("%d", &t);
while(t--)
{
scanf("%d%d", &m, &n);
memset(G, , sizeof(G));
memset(A, , sizeof(A));
k = ;
for(i = ; i <= m ; i++)
{
getchar();
for(j = ; j <= n ; j++)
{
scanf("%c", &maps[i][j]);
if(maps[i][j] == '*')
{
A[i][j] = k;//A[i][j]表示坐标为(i,j)这个城市的编号
k++;
}
}
}//统计城市个数,并对其进行编号
k -= ;
for(i = ; i <= m ; i++)
{
for(j = ; j <= n ; j++)
{
if(A[i][j])//每找到一个城市便根据其对其四周进行构图
Build(i, j);
}
}
int ans = ;
memset(used, , sizeof(used));
for(i = ; i <= k ; i++)
{
memset(vis, , sizeof(vis));
if(Find(i))
ans++;
}
printf("%d\n", k - ans / );
}
return ;
}
/*
2
3 3
*oo
***
o*o
*/
poj 3020 Antenna Placement(最小路径覆盖 + 构图)的更多相关文章
- poj 3020 Antenna Placement (最小路径覆盖)
链接:poj 3020 题意:一个矩形中,有n个城市'*'.'o'表示空地,如今这n个城市都要覆盖无线,若放置一个基站, 那么它至多能够覆盖本身和相邻的一个城市,求至少放置多少个基站才干使得全部的城市 ...
- 二分图最大匹配(匈牙利算法) POJ 3020 Antenna Placement
题目传送门 /* 题意:*的点占据后能顺带占据四个方向的一个*,问最少要占据多少个 匈牙利算法:按坐标奇偶性把*分为两个集合,那么除了匹配的其中一方是顺带占据外,其他都要占据 */ #include ...
- 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——————【 最小路径覆盖、奇偶性建图】
Antenna Placement Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u S ...
- poj 3020 Antenna Placement (最小路径覆盖)
二分图题目 当时看到网上有人的博客写着最小边覆盖,也有人写最小路径覆盖,我就有点方了,斌哥(kuangbin)的博客上只给了代码,没有解释,但是现在我还是明白了,这是个最小路径覆盖(因为我现在还不知道 ...
- POJ 3020 Antenna Placement (二分图最小路径覆盖)
<题目链接> 题目大意:一个矩形中,有N个城市’*’,现在这n个城市都要覆盖无线,每放置一个基站,至多可以覆盖相邻的两个城市.问至少放置多少个基站才能使得所有的城市都覆盖无线? 解题分析: ...
- POJ 3020 Antenna Placement 【最小边覆盖】
传送门:http://poj.org/problem?id=3020 Antenna Placement Time Limit: 1000MS Memory Limit: 65536K Total ...
- POJ - 3020 Antenna Placement(最小覆盖路径)
---恢复内容开始--- https://vjudge.net/problem/POJ-3020 题意 *--代表城市,o--代表空地 给城市安装无线网,一个无线网最多可以覆盖两座城市,问覆盖所有城市 ...
- poj 3020 Antenna Placement(二分无向图 匈牙利)
Antenna Placement Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6438 Accepted: 3176 ...
随机推荐
- AWS 之Load Balance篇
public class CreateELB { /// <summary> /// 连接AWS服务器 /// </summary> /// <param name=&q ...
- jquery radio 取值 取消选中 赋值
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- HDU 3068 (Manacher) 最长回文
求一个字符串的最长子串,Manacher算法是一种O(n)的算法,很给力! s2[0] = '$',是避免在循环中对数组越界的检查. 老大的代码: http://www.cnblogs.com/Big ...
- Flume环境部署和配置详解及案例大全
flume是一个分布式.可靠.和高可用的海量日志采集.聚合和传输的系统.支持在日志系统中定制各类数据发送方,用于收集数据;同时,Flume提供对数据进行简单处理,并写到各种数据接受方(比如文本.HDF ...
- (转) error: linker command failed with exit code 1 (use -v to see invocation)
转自:http://blog.csdn.net/tiantian1980/article/details/9175777 像这样的一大堆,总体说编译链接时错误 /Users/zhangtianji ...
- IOS中微博正文开发步骤总结
微博正文开发步骤总结 1.新建正文控制器,在点击首页的某一条微博时跳转过去 2.在MainController中设置导航控制器的代理,监听所有导航控制器的跳转 1> 如果即将显示的不是根控制器 ...
- LeetCode Count Primes 求素数个数(埃拉托色尼筛选法)
题意:给一个数n,返回小于n的素数个数. 思路:设数字 k =from 2 to sqrt(n),那么对于每个k,从k2开始,在[2,n)范围内只要是k的倍数的都删掉(也就是说[k,k2)是不用理的, ...
- (六)6.16 Neurons Networks linear decoders and its implements
Sparse AutoEncoder是一个三层结构的网络,分别为输入输出与隐层,前边自编码器的描述可知,神经网络中的神经元都采用相同的激励函数,Linear Decoders 修改了自编码器的定义,对 ...
- ArcEngine 通过SpatialRelDescription删除不相交要素
ISpatialFilter.SpatialRel设置为esriSpatialRelRelate,并且设置SpatialRelDescription为某个字符串.该字符串的构造方法:该字符串为长度为9 ...
- CleanMyMac2清理 lanchpad里面的图标没了
好吧.用CleanMyMac2 清理了系统(10.9)之后图标没了.解决办法是: Launchpad存储在一个SQLite数据库中,存储目录是: ~/Library/Application Suppo ...