poj 3020 Antenna Placement

来源:互联网 发布:java 判断日期相等 编辑:程序博客网 时间:2024/06/14 02:37
Time Limit: 1000MSMemory Limit: 65536KTotal Submissions:4297Accepted: 2121

Description

The GlobalAerial Research Centre has been allotted the task of building thefifth generation of mobile phone nets in Sweden. The most strikingreason why they got the job, is their discovery of a new, highlynoise resistant, antenna. It is called 4DAir, and comes in fourtypes. Each type can only transmit and receive signals in adirection aligned with a (slightly skewed) latitudinal andlongitudinal grid, because of the interacting electromagnetic fieldof the earth. The four types correspond to antennas operating inthe directions north, west, south, and east, respectively. Below isan example picture of places of interest, depicted by twelve smallrings, and nine 4DAir antennas depicted by ellipses coveringthem.
poj <wbr>3020 <wbr>Antenna <wbr>Placement
Obviously, it is desirable to use as few antennas as possible, butstill provide coverage for each place of interest. We model theproblem as follows: Let A be a rectangular matrix describing thesurface of Sweden, where an entry of A either is a point ofinterest, which must be covered by at least one antenna, or emptyspace. Antennas can only be positioned at an entry in A. When anantenna is placed at row r and column c, this entry is consideredcovered, but also one of the neighbouring entries(c+1,r),(c,r+1),(c-1,r), or (c,r-1), is covered depending on thetype chosen for this particular antenna. What is the least numberof antennas for which there exists a placement in A such that allpoints of interest are covered?

Input

On the firstrow of input is a single positive integer n, specifying the numberof scenarios that follow. Each scenario begins with a rowcontaining two positive integers h and w, with 1 <=h <= 40 and 0 < w <=10. Thereafter is a matrix presented, describing the points ofinterest in Sweden in the form of h lines, each containing wcharacters from the set ['*','o']. A '*'-character symbolises apoint of interest, whereas a 'o'-character represents openspace.

Output

For eachscenario, output the minimum number of antennas necessary to coverall '*'-entries in the scenario's matrix, on a row of itsown.

Sample Input

27 9ooo**oooo**oo*ooo*o*oo**o**ooooooooo*******ooo*o*oo*oo*******oo10 1***o******

Sample Output

175
 

题意:一个矩形中,有n个城市,现在这n个城市都要覆盖无线,若放置一个基站,那么它至多可以覆盖相邻的两个城市。问至少放置多少个基站才能使得所有的城市都覆盖无线?
 
思路:匈牙利算法,每两个合法(相邻)的星号连一条边,很明显这是一个最小覆盖的问题,但是在构图过程中,匹配是双向的,即<u,v> 和<v,u>都算匹配,因此匹配数多了1倍,所以要除以2 
 
代码:
#include<stdio.h>
#include<string.h>
#include<math.h>

#define Left_Max 501
#define Right_Max 501
#define M 30000
struct node
{
 int x;
 int y;
}point[M];

int N,K;
int visit[M];
int link[M];
int map[Left_Max][Right_Max];
int find_augment(int left,int N)
{
 int i;
 for(i = 1;i <=N;i++)
  if(!visit[i] && map[left][i] != 0)
  {
   visit[i] = 1;
   if(link[i] == 0 || find_augment(link[i],N))
   {
    link[i] = left;
    return 1;
   }
  }
  return 0;
}

int main() 
   
 int i,j;
 int ans,num;
 int t;
 char s[30];
 scanf("%d",&t);
 while(t--)
 {
  
  ans=0;
  num=0;
  scanf("%d%d", &N,&K);  
  memset(link,0,sizeof(link));
  memset(map,0,sizeof(map));  
        for(i = 0; i < N; i++)
  
   
            scanf("%s",s);
   for(j=0;j<K;j++)
   {
    if(s[j]=='*')
    
     num++;
     point[num].x=i;
     point[num].y=j;
     
    }
   }
           
        }
  for(i=1;i<=num;i++)
  {
   for(j=1;j<=num;j++)
   {
    if((abs(point[i].x-point[j].x)+abs(point[i].y-point[j].y))==1)
     map[i][j]=1;
   }
  }
  for(i = 1;i<=num;i++)
  {
   
   memset(visit,0,sizeof(visit));
   if(find_augment(i,num))
    ans++;
  }
  printf("%d\n",num-ans/2);
   
    return 0;