Poj 3020 antenna placement, minimum path overwrite of the Bipartite Graph

Source: Internet
Author: User

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?




Least path overwrite of undirected bipartite graph = number of vertices-Maximum number of bipartite matching/2

Path overwrite is to find some paths in the graph to overwrite all vertices in the graph, and any vertex has only one path associated with it;



#include<cstdio>#include<cstring>#include<vector>#include<algorithm>using namespace std;const int maxn = 500 + 5;struct BPM {    int n, m;    vector<int> G[maxn];    int link[maxn];    bool vis[maxn];    void init(int n)    {        for(int i=0; i<=n+10; ++i) G[i].clear();    }    void AddEdge(int u, int v)    {        G[u].push_back(v);    }    bool match(int u)    {        for(int i=0; i<G[u].size(); ++i) {            int v = G[u][i];            if(!vis[v]) {                vis[v] = true;                if(link[v]==-1 || match(link[v])) {                    link[v] = u;                    return true;                }            }        }        return false;    }    int solve(int n)    {        this->n = n;        memset(link, -1, sizeof link );        int ans = 0;        for(int u=1; u<=n; ++u) {            memset(vis, 0, sizeof vis );            if(match(u)) ans++;        }        return ans;    }};BPM solver;int mtx[maxn][maxn];int dir[4][2]={{-1,0}, {1,0}, {0,1}, {0,-1} };int main(){    int t, h, w;    scanf("%d", &t);    while(t--) {        scanf("%d%d", &h, &w);        solver.init(h*w);        memset(mtx, 0, sizeof mtx );        int i, j;        char str[maxn];        int cnt = 0;        for(i=1; i<=h; ++i) {            scanf("%s", str);            for(j=0; j<w; ++j) {                if(str[j]=='*')                    mtx[i][j+1] = ++cnt;            }        }        for(i=1; i<=h; ++i)            for(j=1; j<=w; ++j)                if(mtx[i][j])                    for(int k=0; k<4; ++k) {                        int xx = i + dir[k][0];                        int yy = j + dir[k][1];                        if(mtx[xx][yy])                            solver.AddEdge(mtx[i][j], mtx[xx][yy]);                    }        int ans = solver.solve(cnt);        printf("%d\n", cnt - ans/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.