zoukankan      html  css  js  c++  java
  • POJ2226(最小顶点覆盖)

    Muddy Fields
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 10044   Accepted: 3743

    Description

    Rain has pummeled the cows' field, a rectangular grid of R rows and C columns (1 <= R <= 50, 1 <= C <= 50). While good for the grass, the rain makes some patches of bare earth quite muddy. The cows, being meticulous grazers, don't want to get their hooves dirty while they eat. 

    To prevent those muddy hooves, Farmer John will place a number of wooden boards over the muddy parts of the cows' field. Each of the boards is 1 unit wide, and can be any length long. Each board must be aligned parallel to one of the sides of the field. 

    Farmer John wishes to minimize the number of boards needed to cover the muddy spots, some of which might require more than one board to cover. The boards may not cover any grass and deprive the cows of grazing area but they can overlap each other. 

    Compute the minimum number of boards FJ requires to cover all the mud in the field.

    Input

    * Line 1: Two space-separated integers: R and C 

    * Lines 2..R+1: Each line contains a string of C characters, with '*' representing a muddy patch, and '.' representing a grassy patch. No spaces are present.

    Output

    * Line 1: A single integer representing the number of boards FJ needs.

    Sample Input

    4 4
    *.*.
    .***
    ***.
    ..*.
    

    Sample Output

    4
    思路:建图,求出横着的board与竖着的board。若两个board相交则建边,那么边其实为'*'。二分图的最小顶点覆盖即为答案。
    #include <cstdio>
    #include <cstring>
    #include <vector>
    using namespace std;
    const int MAXN=1005;
    int n,m;
    char mz[MAXN][MAXN];
    int set_x[MAXN][MAXN],lenx;
    int set_y[MAXN][MAXN],leny;
    vector<int> arc[MAXN];
    void build_graph()
    {
        memset(set_x,0,sizeof(set_x));
        memset(set_y,0,sizeof(set_y));
        lenx=0;
        leny=0;
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                int tag=0;
                if(mz[i][j]=='*')
                {
                    while(j<m&&mz[i][j]=='*')
                    {
                        if(!tag)
                        {
                            tag=1;
                            lenx++;
                        }
                        set_x[i][j]=lenx;
                        j++;
                    }    
                }
            }
        }
        for(int j=0;j<m;j++)
        {
            for(int i=0;i<n;i++)
            {
                int tag=0;
                if(mz[i][j]=='*')
                {
                    while(i<n&&mz[i][j]=='*')
                    {
                        if(!tag)
                        {
                            tag=1;
                            leny++;
                        }    
                        set_y[i][j]=leny;
                        i++;
                    }
                }
            }
        }
        
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)    
            {
                if(mz[i][j]=='*')
                {
                    int u=set_x[i][j];
                    int v=set_y[i][j]+lenx;
                    arc[u].push_back(v);
                    arc[v].push_back(u);
                }
            }
        }
    }
    
    int match[MAXN],vis[MAXN];
    bool dfs(int u)
    {
        for(int i=0;i<arc[u].size();i++)
        {
            int to=arc[u][i];
            if(!vis[to])
            {
                vis[to]=1;
                int w=match[to];
                if(w==-1||dfs(w))
                {
                    match[to]=u;
                    match[u]=to;
                    return true;
                }
            }    
        }
        return false;
    }
    int max_flow()
    {
        int ans=0;
        memset(match,-1,sizeof(match));
        for(int i=1;i<=lenx;i++)
        {
            if(match[i]==-1)
            {
                memset(vis,0,sizeof(vis));
                if(dfs(i))    ans++;
            }    
        }
        return ans;
    } 
    int main()
    {
        while(scanf("%d%d",&n,&m)!=EOF)
        {
            for(int i=0;i<MAXN;i++)    arc[i].clear();
            for(int i=0;i<n;i++)
            {
                scanf("%s",mz[i]);
            }
            build_graph();
            int res=max_flow();
            printf("%d
    ",res);
        }
        return 0;
    }
  • 相关阅读:
    Write an algorithm such that if an element in an MxN matrix is 0, its entire row and column is set to 0.
    旋转二维数组
    replace empty char with new string,unsafe method和native implementation的性能比较
    判断一字符串是否可以另一字符串重新排列而成
    移除重复字符的几个算法简单比较
    也来纠结一下字符串翻转
    判断重复字符存在:更有意义一点
    程序员常去网站汇总
    sublime
    针对程序集 'SqlServerTime' 的 ALTER ASSEMBLY 失败,因为程序集 'SqlServerTime' 未获授权(PERMISSION_SET = EXTERNAL_ACCESS)
  • 原文地址:https://www.cnblogs.com/program-ccc/p/5839766.html
Copyright © 2011-2022 走看看