POJ3020 二分图匹配——最小路径覆盖

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

大意:方格地图上有一些点用 '*' 表示,一个椭圆可以覆盖两个相邻的点(上下左右),问最少用多少椭圆能覆盖所有点

解法每个 '*' 拆为两个点,能够同时覆盖的点连边,构成二分图,跑一边匹配。
要使用的椭圆数='*'总数 - 匹配数 + floor(匹配数/2)
原因:匹配数/2为 要求每个椭圆覆盖两个'*'时 能够使用的最大椭圆数。
这样覆盖后还剩下(
总数 - 匹配数)个'*',对于每个剩下的'*',只能再使用一个椭圆。

提交三次
第一次选错编译器,第二次邻接表没有清空
第三次
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<ctime>
 5 #include<cstdlib>
 6 #include<algorithm>
 7 #include<cmath>
 8 using namespace std;
 9 int read(){
10     int xx=0,ff=1;char ch=getchar();
11     while(ch>'9'||ch<'0'){if(ch=='-')ff=-1;ch=getchar();}
12     while(ch>='0'&&ch<='9'){xx=(xx<<3)+(xx<<1)+ch-'0';ch=getchar();}
13     return xx*ff;
14 }
15 const int ws_[4]={-1,0,1,0},ad_[4]={0,1,0,-1};
16 int H,W,T,id[45][15],tx,ty,sum,ans;
17 char mp[45][15];
18 int lin[1010],len;
19 struct edge{
20     int next,y;
21 }e[1000010];
22 inline void insert(int xx,int yy){
23     e[++len].next=lin[xx];
24     lin[xx]=len;
25     e[len].y=yy;
26 }
27 int vis[1010],tim,pretim,match[1010];
28 bool hun(int x){
29     for(int i=lin[x];i;i=e[i].next)
30         if(vis[e[i].y]<=pretim){
31             vis[e[i].y]=++tim;
32             if(match[e[i].y]==0||hun(match[e[i].y])){
33                 match[e[i].y]=x;
34                 match[x]=e[i].y;
35                 return 1;
36             }
37         }
38     return 0;
39 }
40 int main(){
41     //freopen("in","r",stdin);
42     //freopen("out","w",stdout);
43     T=read();
44     while(T--){
45         H=read(),W=read();
46         for(int i=1;i<=H;i++){
47             for(int j=1;j<=W;j++)
48                 mp[i][j]=getchar(),id[i][j]=(i-1)*W+j;
49             getchar();
50         }
51         len=0;
52         memset(lin,0,sizeof(lin));
53         for(int i=1;i<=H;i++)
54             for(int j=1;j<=W;j++)
55                 if(mp[i][j]=='*')
56                     for(int k=0;k<4;k++){
57                         tx=i+ws_[k],ty=j+ad_[k];
58                         if(tx<=0||tx>H||ty<=0||ty>W)
59                             continue;
60                         if(mp[tx][ty]=='*')
61                             insert(id[i][j],id[tx][ty]+H*W);
62                     }
63         tim=0;sum=0;ans=0;
64         memset(vis,0,sizeof(vis));
65         memset(match,0,sizeof(match));
66         for(int i=1;i<=H;i++)
67             for(int j=1;j<=W;j++)
68                 if(mp[i][j]=='*'){
69                     sum++;
70                     pretim=tim;
71                     vis[id[i][j]]=++tim;
72                     if(hun(id[i][j]))
73                         ans++;
74                 }
75         printf("%d\n",sum-ans+ans/2);
76     }
77     return 0;
78 }

 

 
 
posted @ 2017-12-09 11:10  咸鱼lzh  阅读(268)  评论(1编辑  收藏  举报