POJ 3020-Antenna Placement-二分图之最小路径覆盖

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

核心思想:

记图中的所有*的个数为cnt,每个点都和上下左右连通的点建立双向边,那么两个集合是一样的,用匈牙利算法求最大匹配数num,则实际的对数应该是num/2。匹配起来的点的个数为num,未匹配起来的点的个数为cnt-num,则答案=未匹配起来的点的个数+匹配的对数,即ans=cnt-num+num/2=cnt-num/2。

代码如下:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
typedef long long ll;
const int N=550,M=1e5+20;
char s[N][N];
int n,m,en;
vector<int>vec[M];
bool vis[M];
int p[M];
bool dfs(int x)
{
	int len=vec[x].size();
	for(int i=0;i<len;i++)
	{
		int y=vec[x][i];
		if(vis[y])continue;
		vis[y]=1;
		if(p[y]==-1||dfs(p[y]))
		{
			p[y]=x;
			return 1;
		}
	}
	return 0;
}
int main()
{
	int T;
	cin>>T;
	while(T--)
	{
		scanf("%d%d",&n,&m);
		en=n*m;
		int cnt=0;
		for(int i=0;i<n;i++)
			scanf("%s",s[i]);
		for(int i=0;i<en;i++)
		{
			p[i]=-1;
			vec[i].clear();
		}
		for(int i=0;i<n;i++)
			for(int j=0;j<m;j++)
			{
				if(s[i][j]!='*')continue;
				cnt++;
				int z=i*m+j;
				if(j+1<m&&s[i][j+1]=='*')
				{
					int t=i*m+j+1;
					vec[z].push_back(t);
					vec[t].push_back(z);
				}
				if(i+1<n&&s[i+1][j]=='*')
				{
					int t=(i+1)*m+j;
					vec[z].push_back(t);
					vec[t].push_back(z);
				}
			}
		int ans=0;
		for(int i=0;i<en;i++)
		{
			for(int j=0;j<en;j++)
				vis[j]=0;
			ans+=dfs(i);
		}
		printf("%d\n",cnt-ans/2);
	}
	return 0;
}
发布了144 篇原创文章 · 获赞 135 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/Nothing_but_Fight/article/details/102981022
今日推荐