博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj 3020 Antenna Placement 匈牙利算法
阅读量:3904 次
发布时间:2019-05-23

本文共 3572 字,大约阅读时间需要 11 分钟。

题目:

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

27 9ooo**oooo**oo*ooo*o*oo**o**ooooooooo*******ooo*o*oo*oo*******oo10 1***o******

Sample Output

175

思路:

如果某个点为'*',则遍历看看它的四个方向上的元素如果也是'*',则在两个顶点建边。当然这需要对元素进行编号。

然后套用匈牙利算法。

题目的答案为最大匹配数加上没有匹配的顶点且为'*'的数目。

因为得出的结果ans为最大匹配数的两倍,所以答案为ans/2+(cnt-ans);(cnt为顶点为'*'的结点总数)。

这个题因为把左移运算符写成右移运算符搞错了,使数组缩小了好几倍,然后越界,调了好长时间。

代码如下:

#include 
#include
#include
#include
using namespace std;const int maxn=15;int h,w,t;char a[maxn<<2][maxn];int id[maxn<<2][maxn];int pos[4][2]={
{1,0},{-1,0},{0,1},{0,-1}};int cnt;int head[maxn*maxn<<2];int match[maxn*maxn<<3];int vis[maxn*maxn<<3];struct edge{ int to; int next;}edge[maxn*maxn<<3];void add (int id,int u,int v){ edge[id].to=v; edge[id].next=head[u]; head[u]=id;}bool Find(int x){ for (int i=head[x];i!=-1;i=edge[i].next) { int u=edge[i].to; if(!vis[u]) { vis[u]=1; if(match[u]==-1||Find(match[u])) { match[u]=x; return true; } } } return false;}int algor(){ int ans=0; memset (match,-1,sizeof(match)); for (int i=1;i<=cnt;i++) { memset (vis,0,sizeof(vis)); if(Find(i)) ans++; } return ans;}int main(){ scanf("%d",&t); while(t--) { memset (head,-1,sizeof(head)); memset (id,0,sizeof(id)); cnt=0; scanf("%d%d",&h,&w); getchar(); int num=0; for (int i=0;i
=0&&tx
=0&&ty

 

转载地址:http://wzoen.baihongyu.com/

你可能感兴趣的文章
win7 动态硬盘
查看>>
Openoffice fedora/linux build
查看>>
openoffice development
查看>>
no office executable found! error still exist after adding /openoffice.org3/program to classpath
查看>>
Eclipse OpenOffice Development Plugin
查看>>
Netbeans 6.5 JDK was not found Unsupported Java VM
查看>>
Netbeans显示行号
查看>>
latex BibTex 引文顺序
查看>>
NetBeans 6.5 新建项目 project folder already exist and is not empty
查看>>
open office (ooffice) 如何使用公式编辑器 (formula)
查看>>
mysql 自动增长 字段 插入
查看>>
使用JDBC时 Class.forName()的作用
查看>>
VBA Collection
查看>>
vba dictionray
查看>>
为vc程序增加mfc支持
查看>>
vc 文件存在 文件属性 删除文件
查看>>
vc彻底删除目录
查看>>
vc error
查看>>
visual studio 2008 a problem has been encountered while loading the setup components
查看>>
visual studio 2008 a problem has been encountered while loading the setup components
查看>>