Bzoj 1735: [usaco2005 Jan] muddy fields muddy pasture

Source: Internet
Author: User
Description

Heavy rain struck dairy farms. farm is a rectangle of R * C, where 1 ≤ r, c ≤ 50. heavy rain makes the land without long grass muddy, but the careful cows don't want to dirty their shoes while grazing. to prevent their shoes from getting dirty, John decided to put some boards in the muddy pasture. the width of each Board is 1 unit and the length is arbitrary. each board must be placed in the mud parallel to the pasture. john wants to use the least plank to cover all the mud. one board can overlap on another board, but it cannot be placed on the grass. input

Row 1st: two integers: R and C.

Lines 2nd to R + 1: Each line contains C characters. "*" indicates the mud, and "." indicates the lawn. output.

Minimum number of boards required.

Question:

? It cannot be well covered, so boards with a width of 1 can only be placed in connected blocks of rows and columns.

? Therefore, the connected blocks of rows and columns correspond to vertices in the left and right sides, and correspond to edges in the mud.

? The minimum coverage of a bipartite graph is the answer.

Bipartite Graph minimum vertex overwrite = maximum match

Code:

#include<cstdio>#include<cstring>#include<algorithm>//by zrt//problem:using namespace std;typedef long long ll;const double eps(1e-10);int R,C,n;char s[60][60];int link[1005];int cover[1005];int H[1005],X[1000050],P[1000050],tot;inline void add(int x,int y){    P[++tot]=y;X[tot]=H[x];H[x]=tot;}bool find(int x){    for(int i=H[x];i;i=X[i]){        if(cover[P[i]]) continue;        cover[P[i]]=1;        int q=link[P[i]];        link[P[i]]=x;        if(q==-1||find(q)) return 1;        link[P[i]]=q;    }    return 0;}int xn,yn;int a[60][60],b[60][60];int main(){    #ifdef LOCAL    freopen("in.txt","r",stdin);    freopen("out.txt","w",stdout);    #endif    memset(link,-1,sizeof link);    scanf("%d%d",&R,&C);    for(int i=0;i<R;i++){        scanf("%s",s[i]);    }    for(int i=0;i<R;i++){        for(int j=0;j<C;j++){            if(s[i][j]==‘*‘){                if(j>0&&s[i][j-1]==‘*‘){                    a[i][j]=a[i][j-1];                }else a[i][j]=++yn;                if(i>0&&s[i-1][j]==‘*‘){                    b[i][j]=b[i-1][j];                }else b[i][j]=++xn;            }        }    }    for(int i=0;i<R;i++){        for(int j=0;j<C;j++){            if(s[i][j]==‘*‘){                    add(a[i][j],b[i][j]);            }        }    }    n=yn;    int ans(0);    for(int i=1;i<=n;i++){        memset(cover,0,sizeof cover);        if(find(i)) ans++;    }    printf("%d\n",ans);    return 0;}

Bzoj 1735: [usaco2005 Jan] muddy fields muddy pasture

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.