poj 3020 Antenna Placement 二分图

来源:互联网 发布:英雄联盟网络延迟高 编辑:程序博客网 时间:2024/06/07 00:43
Antenna Placement
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 7223 Accepted: 3591

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

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

Sample Output

175

Source

Svenskt Mästerskap i Programmering/Norgesmesterskapet 2001

题意就是有长度为2的两个点,问最少需要几个能全部覆盖


求最小路径覆盖
最小路径覆盖=顶点数-最大匹配

证明:
1  如果匹配数为0,那么图中没有边,需要n条路径

2  如果a,b之间连一条边,那么匹配数增1,需要的路径数会减少一,因为a,b之间只需要一条,那么就证明了

我的code中 最大匹配求的是双向的,所以/2

#include <iostream>#include <stdio.h>#include <algorithm>#include <stdlib.h>#include <math.h>#include <string.h>#include <vector>#include <map>#include <set>#include <queue>#include <stack>using namespace std;#define Max 500int T;int cx[Max],cy[Max],w,h,grid[Max][Max],v1,v2,vis[Max],cnt;int mp[Max][Max];char t;int xn[5]={0,-1,0,1,0};int ynn[5]={0,0,1,0,-1};bool dfs(int u){for(int v=1;v<=v2;v++){if(grid[u][v]&&!vis[v]){vis[v]=1;if(cy[v]==0||dfs(cy[v])){cx[u]=v;cy[v]=u;return true;}}}return false;}int main(){cin>>T;while(T--){cin>>h>>w;v1=v2=cnt=0;memset(grid,0,sizeof(grid));memset(cx,0,sizeof(cx));memset(cy,0,sizeof(cy));memset(mp,0,sizeof(mp));for(int i=1;i<=h;i++){for(int j=1;j<=w;j++){cin>>t;if(t=='*')mp[j][i]=++cnt;}}for(int j=1;j<=h;j++){for(int i=1;i<=w;i++){if(mp[i][j]!=0)for(int k=1;k<=4;k++){int xx=i+xn[k];int yy=j+ynn[k];if(xx>=1&&xx<=w&&yy>=1&&yy<=h){if(mp[xx][yy]){grid[mp[i][j]][mp[xx][yy]]=1;}}}}}int res=0;v1=v2=cnt;for(int i=1;i<=v1;i++){memset(vis,0,sizeof(vis));if(dfs(i))res++;}cout<<cnt-res/2<<endl;}return 0;}





0 0
原创粉丝点击