POJ3020 Antenna Placement(二分图最小路径覆盖)
2024-08-27 03:53:27
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?
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
题意:一个n*m的平面内有一些点,有1*2的纸条,可以横放或竖放,求最少用多少张纸条才能覆盖所有点?
题解:纸条相当于一条边,上道题求得为覆盖所有边的最小点数,这道题则逆其道而行,可转化为覆盖所有点的最小边数,即最小路径覆盖
二分图中最小路径覆盖=点数-最小边覆盖
然后就可以用匈牙利跑了~
代码如下:
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std; vector<int> g[];
int vis[],link[];
int map[][],n,m,cnt,ttt;
char c[][]; int dfs(int x)
{
int sz=g[x].size();
for(int k=;k<sz;k++)
{
int y=g[x][k];
if(!vis[y])
{
vis[y]=;
if(!link[y]||dfs(link[y]))
{
link[y]=x;
return ;
}
}
}
return ;
} int search()
{
memset(link,,sizeof(link));
int tmp=;
for(int i=;i<=cnt;i++)
{
if(dfs(i))
{
memset(vis,,sizeof(vis));
tmp++;
}
}
return tmp;
} int main()
{
scanf("%d",&ttt);
while(ttt--)
{
cnt=;
scanf("%d%d",&n,&m);
memset(map,,sizeof(map));
for(int i=;i<=;i++)
{
g[i].clear();
}
for(int i=;i<=n;i++)
{
scanf("%s",c[i]);
for(int j=;j<m;j++)
{
if(c[i][j]=='o')
{
map[i][j+]=;
}
else
{
map[i][j+]=++cnt;
}
}
} for(int i=;i<=n;i++)
{
for(int j=;j<=m;j++)
{
if(map[i][j])
{
if(map[i][j+])
{
g[map[i][j]].push_back(map[i][j+]);
}
if(map[i][j-])
{
g[map[i][j]].push_back(map[i][j-]);
}
if(map[i+][j])
{
g[map[i][j]].push_back(map[i+][j]);
}
if(map[i-][j])
{
g[map[i][j]].push_back(map[i-][j]);
}
}
}
}
int ans=search();
int x=cnt-ans/;
printf("%d\n",x);
}
}
最新文章
- LoadRunner之篇
- C#~异步编程在项目中的使用
- ie7下<;a href=";javascript:;";>;标签不反应
- Redis学习笔记七:独立功能之排序
- iOS开发——高级篇——地图 MapKit
- uGUI练习(七) Drag And Drop
- MySQL逻辑备份与恢复
- jquery easyui Combobox 实现 两级联动
- Java初始化(成员变量)
- 关于在asp.net的web页面中的全局变量问题
- Android01--开发环境搭建
- oracle 排序
- sublimeText3安装package control和禁止弹出更新下载弹窗
- Java中数组的初始化方式
- VS2008下直接安装使用Boost库1.46.1版本号
- Spring MVC+JSP实现三级联动
- Android NDK开发及调用标准linux动态库.so文件
- EF的join用法
- Linux下PHP连接MS SQLServer的办法
- yii pageTitle与Yii::app()->;name的区别