【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??
?
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題目大意:
*--代表城市,o--代表空地
給城市安裝無線網(wǎng),一個無線網(wǎng)最多可以覆蓋兩座城市,問覆蓋所有城市最少要用多少無線。
解題報告:
有個想法很不錯的,幫助理解最小路徑覆蓋博客
看了這個題解!!我算是徹底懂了無向圖的最小邊覆蓋的求解過程以及為什么是這么求解,但是博客中有個地方說錯了,她說是拆點(diǎn)后變成無向圖了,,但是其實(shí)是原來就是個無向圖(也就是說原博客說拆點(diǎn)后變成雙向圖了更合適),拆點(diǎn)后變成了兩個完全一樣的無向圖,所以可以最后結(jié)果直接除以2,(其實(shí)是建圖的(二分圖的所有頂點(diǎn)-最大匹配)/2? 。化簡一步才變成了:? ? 復(fù)制一遍圖之前的頂點(diǎn)(也就是原始圖的頂點(diǎn))- (最大匹配/2))題解如下:神奇啊二分圖https://blog.csdn.net/mmy1996/article/details/52289564
但是最小路徑覆蓋的n都按照拆點(diǎn)之前的算。。。。
無向二分圖的最小邊覆蓋 = 頂點(diǎn)數(shù) – 最大二分匹配數(shù)/2
頂點(diǎn)數(shù):就是用于構(gòu)造無向二分圖的城市數(shù),即*的數(shù)量
最大二分匹配書之所以要除以2,是因?yàn)闊o向圖匹配是雙向的,因此除以2得到原圖的真正的匹配數(shù)
AC代碼:
#include<cstdio> #include<iostream> #include<cstring> using namespace std;const int MAX = 505; int n,m,tot; bool line[MAX][MAX],used[MAX]; int nxt[MAX],vis[MAX][MAX]; char maze[MAX][MAX]; bool find(int x) {for(int i = 1; i<=tot; i++) {if(line[x][i] && used[i] == 0) {used[i] = 1;if(nxt[i] == -1 || find(nxt[i])) {nxt[i] = x;return 1;}}}return 0 ; } int match() {int sum = 0;memset(nxt,-1,sizeof nxt);for(int i = 1; i<=tot; i++) {memset(used,0,sizeof used);if(find(i)) sum++;}return sum; } int main() {int t;cin>>t;while(t--) {tot=0;scanf("%d%d",&n,&m);//n行m列memset(line,0,sizeof line);memset(vis,0,sizeof vis);for(int i = 1; i<=n; i++) scanf("%s",maze[i]+1);for(int i = 1; i<=n; i++) {for(int j = 1; j<=m; j++) {if(maze[i][j] == '*') vis[i][j] = ++tot;}} for(int i = 1; i<=n; i++) {for(int j = 1; j<=m; j++) {if(maze[i][j] == '*') {if(i-1 >= 1 && maze[i-1][j] == '*') line[vis[i][j]][vis[i-1][j]]=1;if(i+1 <= n && maze[i+1][j] == '*') line[vis[i][j]][vis[i+1][j]]=1;if(j-1 >= 1 && maze[i][j-1] == '*') line[vis[i][j]][vis[i][j-1]]=1;if(j+1 <= m && maze[i][j+1] == '*') line[vis[i][j]][vis[i][j+1]]=1;}}}printf("%d\n",tot - (match()/2));}return 0 ;}?
總結(jié)
以上是生活随笔為你收集整理的【POJ - 3020】Antenna Placement (匈牙利算法,二分图最小边覆盖)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 10268元!华为IdeaHub Boa
- 下一篇: *【HDU - 2586】How far