POJ 3020 Antenna Placement

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? 

题目大意:

一个矩形中,有N个城市’*’,现在这n个城市都要覆盖无线,若放置一个基站,那么它至多可以覆盖相邻的两个城市。
问至少放置多少个基站才能使得所有的城市都覆盖无线?


这道题是一个二分图求最大覆盖路径,难点在于建图,要将所有的'*',也就是城市重新标号,一个城市可以和他相邻的城市相邻,由此建图,则可以按照二分图用匈牙利算法计算,还有一点注意的是,建图的时候要建成无向的二分图,即map[i][j]=map[j][i]=1,这样建图后计算的最大匹配数要除以2才是真正的匹配数。

#include<stdio.h>
#include<string.h>
char map[51][50];
int mm[510][510];
int use[500],line[510];
int id[51][51];
int x[5]={1,-1,0,0};
int y[5]={0,0,1,-1};
int cot,n,m;
int find(int x);
int ok(int a,int b);
int main()
{
	int t,i,j,k;
	scanf("%d",&t);
	while(t--)
	{
		cot=0;
		memset(mm,0,sizeof(mm));
		memset(line,-1,sizeof(line));
		scanf("%d%d",&n,&m);
		for(i=0;i<n;i++)
		{
			scanf("%s",map[i]);
			for(j=0;j<m;j++)
			{
				if(map[i][j]=='*')
					id[i][j]=cot++;//将左右的城市重新标号
			}
		}
		for(i=0;i<n;i++)  //搜索可以相连的城市,建立二分图
		{
			for(j=0;j<m;j++)
			{
				if(map[i][j]=='*')
				{
					int u,v;
					u=id[i][j];
					for(k=0;k<4;k++)
					{
						int a,b;
						a=i+x[k];
						b=j+y[k];
						if(ok(a,b))
						{
							if(map[a][b]=='*')
							{
								v=id[a][b];
								mm[u][v]=1;  //无向图
								mm[v][u]=1;
							}
						}
					}
				}

			}
		}
		int ans=0;
		for(i=0;i<cot;i++)
		{
			memset(use,0,sizeof(use));
			if(find(i))
				ans++;
		}
		printf("%d\n",cot-ans/2);
	}
	return 0;
}
int ok(int a,int b)
{
	if(a>=0&&a<n&&b>=0&&b<m)
		return 1;
	return 0;
}
int find(int x)  //匈牙利算法
{
	int i,j,k;
	for(i=0;i<cot;i++)
	{
		if(!use[i]&&mm[x][i])
		{
			use[i]=1;
			if(line[i]==-1||find(line[i]))
			{
				line[i]=x;
				return 1;
			}
		}
	}
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值