您的位置:首页 > 其它

poj 3020 Antenna Placement 二分图最小路径覆盖

2014-03-31 20:03 429 查看
Antenna Placement

Time Limit: 1000MS Memory Limit: 65536K

Total Submissions: 6126 Accepted: 3029

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'为空地,现有信号发射装置,装在一个地点的信号发射装置可以用信号多覆盖上、下、左或右边相邻的一个地点,但是一个信号发射装置只能固定覆盖一个方向。问最少需要多少个信号发射装置才能覆盖所有的地点,也就是’*‘。

解题思路:用最少的边覆盖所有的点,孤点视为一个边,这正是最小路径覆盖。但是题中的图并不是二分图,不方便直接用匈牙利算法,所以要拆点。然后使用二分图最大匹配求最小路径覆盖的公式:最小路径覆盖=顶点数-最大匹配数。//Memory: 580K Time: 0MS

#include<cstdio>
#include<vector>
using namespace std;
const int MAX = 400; //最多有40*10个地点
int H , W , N , linker[MAX] , g0[40][10]; //需要记录特定坐标的地点编号
bool used[MAX];
vector<int> g[MAX]; //建立邻接表
//匈牙利算法
bool dfs(int v)
{
for( int i = 0 ; i < g[v].size() ; i++ )
{
int u = g[v][i];
if( !used[u] )
{
used[u] = true;
if( linker[u] == -1 || dfs(linker[u]) )
{
linker[u] = v;
return true;
}
}
}
return false;
}

int hungary()
{
int res = 0;
for( int i = 0 ; i < N ; i++ )
linker[i] = -1;
for( int i = 0 ; i < N ; i++ )
{
for( int j = 0 ; j < N ; j++ )
used[j] = false;
if( dfs(i) )
res++;
}
return res;
}

int main()
{
int caseNum;
scanf("%d", &caseNum);
while(caseNum--)
{
scanf("%d%d", &H , &W);
N = 0;
for( int i = 0 ; i < H ; i++ )
{
char str[11]; //逐行接入字符串比逐个接入字符更快一些
scanf("%s", str); //但是这道题里字符少,差别应该并不大
for( int j = 0 ; j < W ; j++ )
g0[i][j] = (str[j] == '*')?N++:-1; //如果为'*'直接存入自增编号,否则置为-1
}
for( int i = 0 ; i < N ; i++ )
g[i].clear(); //初始化邻接表
for( int i = 0 ; i < H ; i++ )
for( int j = 0 ; j < W ; j++ )
if( g0[i][j] > -1 ) //如果当前坐标是'*'
{
if( j+1 < W && g0[i][j+1] > -1 ) //如果它的右侧还未越界且也是'*'
{
int a = g0[i][j];
int b = g0[i][j+1];
g[a].push_back(b); //在邻接表中建立双向边
g[b].push_back(a);
}
if( i+1 < H && g0[i+1][j] > -1 ) //如果它的下侧还未越界且也是'*'
{
int a = g0[i][j];
int b = g0[i+1][j];
g[a].push_back(b); //在邻接表中建立双向边
g[b].push_back(a);
}
}
printf("%d\n", N-hungary()/2); //拆点使用匈牙利算法需要除2
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息