H - Antenna Placement(匈牙利算法+拆点+无向二分图建立)
2017-10-12 19:55
176 查看
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
Sample Output
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
为了把有向图G构造为无向二分图,这里需要引入一个新名词“拆点” 其实就是把原有向图G的每一个顶点都”拆分(我认为复制更准确)”为2个点,分别属于所要构造的二分图的两个顶点集 最大二分匹配书之所以要除以2,是因为进行了“拆点”擦奥做做使得匹配总数多了一倍,因此除以2得到原图的真正的匹配数
#include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <algorithm> using namespace std; char ma[400][400]; int kma[400][400]; bool pma[400][400]; bool v[434]; int link[421]; int n, m, k, M; int xx[] = {0, 1, -1, 0}; int yy[] = {1, 0, 0, -1}; bool dfs(int x) { for(int y=1;y<=k;y++) { if(pma[x][y]&&!v[y]) { v[y] = true; if(link[y]==0||dfs(link[y])) { link[y] = x; return true; } } } return false; } void Search() { for(int x = 1;x<=k;x++) { memset(v, false, sizeof(v)); if(dfs(x)) M++; } return ; } int main() { int T; scanf("%d", &T); while(T--) { scanf("%d %d", &n, &m); memset(link, 0, sizeof(link)); k = 0; M = 0; for(int i=0;i<n;i++) { scanf("%s", ma[i]);//存图 } memset(kma, 0, sizeof(kma)); memset(pma, false, sizeof(pma)); for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { if(ma[i][j]=='*') kma[i][j] = ++k;//建立需要匹配的图 } } for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { if(kma[i][j])//匹配 { for(int ka = 0; ka<4;ka++) { int x = xx[ka] + i; int y = yy[ka] + j; if(kma[x][y]) { pma[kma[i][j]][kma[x][y]] = true; } } } } } Search(); printf("%d\n", k-M/2);//最小路径覆盖=G-最大匹配数 } return 0; }
相关文章推荐
- 51nod2006 飞行员配对(二分图最大匹配)匈牙利算法
- 2017秦皇岛现场赛H.Prime Set(奇偶二分图 匈牙利算法)
- 二分图的最大匹配、完美匹配和匈牙利算法
- POJ1469[COURSES] 二分图最大匹配 匈牙利算法
- The Perfect Stall 完美的牛栏 二分图最大匹配,匈牙利算法
- hdu 2063过山车-二分图 匈牙利算法
- 二分图最大匹配问题与匈牙利算法的核心思想
- 二分图-匈牙利算法(dfs实现)
- 二分图的最大匹配、完美匹配和匈牙利算法
- "《算法导论》之‘图’":不带权二分图最大匹配(匈牙利算法)
- #1122 : 二分图二•二分图最大匹配之匈牙利算法
- 二分图的最大匹配 (匈牙利算法)
- 【二分图】【最大匹配】【匈牙利算法】洛谷 P2071 座位安排 seat.cpp/c/pas
- POJ-1325-Machine Schedule-最小点覆盖(二分图-匈牙利算法)
- 51nod 2006 飞行员配对(二分图最大匹配) 裸匈牙利算法 求二分图最大匹配题
- Pku acm 1469 COURSES 数据结构题目解题报告(十一)---- 匈牙利算法求二分图的最大匹配
- POJ 3041 Asteroids 匈牙利算法 二分图最大匹配
- 匈牙利算法 二分图
- 二分图的匈牙利算法与常用定理小节
- 二分图 匈牙利算法