POJ3020 Antenna Placement 二分匹配 匈牙利算法进阶(纪念1A了个稍微难点的题目)

题源:http://poj.org/problem?id=3020
题意:给你一张地图,一个圈只能圈相邻的两个点,给你点的坐标,问你所有点至少需要多少个圈才能全覆盖。
刚开始没思路,搜了一下题目(只看了标题噢),看都说是匈牙利算法,才想到这个就是类似于匈牙利算法的。因为一个圈恰好能圈起来两个,就是二部图嘛~
这题自己用的数据结构有点乱,有存二维坐标用的数组,有地图(所以像是搜索),有匈牙利算法的各个数组,还得变成二维的。大体是匈牙利的板子再把它二维化再思索修改一下就是这样了。
不过不是cnt-ans/2了 应该是cnt-ans,因为加了一条,对于已经匹配好的,不去动它,所以ans本来就没有统计两次~

下面上1A代码

#include
#include
#include
#include
#include
#include
#include
#define ll long long
#define inf 0x3f3f3f3f
#define MID (t[k].l+t[k].r)>>1
#define cl(a,b) memset(a,b,sizeof(a))
using namespace std;
int h,w;
char map[50][12];
bool used[50][12];//标记这个点是否被覆盖过
struct node{
	int x,y;
}cp[50][12];//表示当前这个点的cp是谁
queue<node> q;//所有的点
int nextt[4][2]={0,1,1,0,0,-1,-1,0};
bool judge(int tx,int ty){//判断当前点是否符合要求
	if(tx>=1&&ty>=1&&tx<=h&&ty<=w&&map[tx][ty]=='*')
		return true;
	return false;
}
bool find(int x,int y){
	for(int i=0;i<4;i++){
		int tx=x+nextt[i][0];
		int ty=y+nextt[i][1];
		if(judge(tx,ty)&&!used[tx][ty]){
			used[tx][ty]=true;
			if(cp[tx][ty].x==-1||find(cp[tx][ty].x,cp[tx][ty].y)){
				cp[x][y].x=tx,cp[x][y].y=ty;
				cp[tx][ty].x=x,cp[tx][ty].y=y;
				return true;
			}
		}
	}
	return false;
} 
int main(){
	int T;
	scanf("%d",&T);
	while(T--){
		cl(cp,-1);
		while(!q.empty()) q.pop();
		node temp;
		int cnt=0;//总共点数
		scanf("%d%d",&h,&w);getchar();
		for(int i=1;i<=h;i++){
			for(int j=1;j<=w;j++){
				map[i][j]=getchar();
				if(map[i][j]=='*'){
					temp.x=i,temp.y=j;
					q.push(temp);
					cnt++;
				}
			}
			getchar();
		}
		int ans=0;
		while(!q.empty()){
			cl(used,0);
			temp=q.front();
			q.pop();
			if(cp[temp.x][temp.y].x!=-1) continue;//这里防止重复统计
			if(find(temp.x,temp.y)) ans++;
		}
		printf("%d\n",cnt-ans);
	}
	return 0;
}

Antenna Placement
Time Limit: 1000MS

Memory Limit: 65536K
Total Submissions: 11978

Accepted: 5890
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*ooo
ooooooooo
****oo
o
o
oo
oo
*******oo
10 1
*
*
*
o
*
*
*
*
*
*
Sample Output
17
5
Source

你可能感兴趣的:(二分图,图论,二分图,图论,匈牙利算法)