Question:
In a rectangle, there are n Cities '*'. Now all N cities need to cover wireless networks. If a base station is placed, it can cover at most two adjacent cities.
How many base stations can be placed to ensure that all cities are wireless?
Ideas:
Number each city, create two-way edge, run Hungary, and then the number of cities-matching number/2 is the answer
Assume that each city needs to build a base station, and then how many matches will reduce the number of base stations. Because the two-way edge is used,/2 is required.
/* ***********************************************Author :devilCreated Time :2016/5/17 11:25:49************************************************ */#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#include <vector>#include <queue>#include <set>#include <map>#include <string>#include <cmath>#include <stdlib.h>using namespace std;char s[42][12];int mp[42][12],link[410];bool vis[410];vector<int>eg[410];bool dfs(int u){ for(int i=0;i<eg[u].size();i++) { int v=eg[u][i]; if(!vis[v]) { vis[v]=1; if(link[v]==-1||dfs(link[v])) { link[v]=u; return 1; } } } return 0;}int main(){ //freopen("in.txt","r",stdin); int t; scanf("%d",&t); while(t--) { int n,m,cnt=0; memset(link,-1,sizeof(link)); scanf("%d%d",&n,&m); for(int i=0;i<n;i++) scanf("%s",s[i]); for(int i=0;i<n;i++) for(int j=0;j<m;j++) if(s[i][j]==‘*‘) mp[i][j]=++cnt; for(int i=1;i<=cnt;i++) eg[i].clear(); for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { if(s[i][j]==‘*‘) { if(i<n-1&&s[i+1][j]==‘*‘) { eg[mp[i][j]].push_back(mp[i+1][j]); eg[mp[i+1][j]].push_back(mp[i][j]); } if(j<m-1&&s[i][j+1]==‘*‘) { eg[mp[i][j]].push_back(mp[i][j+1]); eg[mp[i][j+1]].push_back(mp[i][j]); } } } } int ans=0; for(int i=1;i<=cnt;i++) { memset(vis,0,sizeof(vis)); ans+=dfs(i); } printf("%d\n",cnt-ans/2); } return 0;}
Poj3020 antenna placement (Minimum side coverage)