Poj 3020 antenna placement Hungary Algorithm

Source: Internet
Author: User

There are n cities in a rectangular grid. Now all N cities need to be covered in wireless networks. For any base station, it can overwrite any adjacent two grids. So if we build a base station together, we can make all the cities covered and use the least number of base stations?

Solution: consider a city as a point, and two adjacent cities are connected to one edge. If an edge exists between A and B, A and B can be covered by the same base station, and each remaining point must be covered by a base station.

Therefore, the total number of Base Stations = minimum edge coverage = maximum binary matching + maximum independent set. However, in a bipartite graph, the set of the left half graph and the set of the right half graph are essentially the same set, so each point of the set is overwritten twice.

If the left half gallery is X, the Right Half gallery is Y, and the maximum number of matching is m, then | x | = | Y |, number of Base Stations = [M + (| x |-M + | Y |-m)]/2 = | x |-M/2;

The following two implementation methods are provided:

#include <iostream>using namespace std;char matrix[50][15];bool map[500][500],vis[500];int match[500]; int dir[4][2] = {{-1,0},{1,0},{0,1},{0,-1}};int h,w;bool find_path ( int t ){int r, c, pos;for ( int i = 0; i < 4; i++ ){r = t / w + dir[i][0];      c = t % w + dir[i][1];pos = r * w + c;if ( r >= 0 && r < h && c >= 0 && c < w && map[t][pos] && !vis[pos] ){vis[pos] = true;if ( match[pos] == -1 || find_path ( match[pos] ) ){match[pos] = t;return true;}}}return false;}int Hungary(){int ans = 0;memset(match,-1,sizeof(match));for ( int i = 0; i < w * h; i++ ){memset(vis,0,sizeof(vis));if ( find_path(i) )ans++;}return ans;}int main(){int i, j, k, r, c, t, cnt;scanf("%d",&t);while ( t-- ){scanf("%d%d",&h,&w);        for ( i = 0; i < h; i++ )scanf("%s",matrix[i]);cnt = 0;memset(map,0,sizeof(map));for ( i = 0; i < h; i++ ){for ( j = 0; j < w; j++ ){if ( matrix[i][j] == 'o' ) continue;for ( k = 0; k < 4; k++ ){r = i + dir[k][0];c = j + dir[k][1];if ( r >= 0 && r < h && c >= 0 && c < w && matrix[r][c] == '*'  )map[i*w+j][r*w+c] = true;}cnt++;}}printf("%d\n", cnt - Hungary() / 2 );}return 0;}

 

 

#include <iostream>using namespace std;int match[500]; int dir[4][2] = {{-1,0},{1,0},{0,1},{0,-1}};bool map[500][500],vis[500];int h, w, cnt;struct{char ch;int id;} matrix[50][15];bool find_path ( int t ){for ( int i = 1; i <= cnt; i++ ){if ( map[t][i] && ! vis[i] ){vis[i] = true;if ( match[i] == -1 || find_path ( match[i] ) ){match[i] = t;return true;}}}return false;}int Hungary(){int i, ans = 0;memset(match,-1,sizeof(match));for ( i = 1; i <= cnt; i++ ){memset(vis,0,sizeof(vis));if ( find_path(i) )ans++;}return ans;}int main(){int i, j, k, r, c, t;scanf("%d",&t);while ( t-- ){scanf("%d%d",&h,&w);       cnt = 0;for ( i = 0; i < h; i++ ){getchar();for ( j = 0; j < w; j++ ){   matrix[i][j].ch = getchar();   if ( matrix[i][j].ch == '*' )   matrix[i][j].id = ++ cnt;}}memset(map,0,sizeof(map));for ( i = 0; i < h; i++ ){for ( j = 0; j < w; j++ ){if ( matrix[i][j].ch == 'o' ) continue;for ( k = 0; k < 4; k++ ){r = i + dir[k][0];c = j + dir[k][1];if ( r >= 0 && r < h && c >= 0 && c < w && matrix[r][c].ch == '*'  )map[ matrix[i][j].id ][ matrix[r][c].id ] = true;}}}printf("%d\n", cnt - Hungary() / 2 );}return 0;}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.