zoukankan      html  css  js  c++  java
  • [POJ2226]Muddy Fields(二分图匹配)

    【原题】

    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

    Hint

    OUTPUT DETAILS:

    Boards 1, 2, 3 and 4 are placed as follows:
    1.2.
    .333
    444.
    ..2.
    Board 2 overlaps boards 3 and 4.
     
    【思路】
    匈牙利算法。横着的连通块和竖着的连通块作为两边需要匹配的点,连通块重合的部分是边。覆盖所有的边即把所有泥浆铺上木板,至此转化为最小覆盖点问题。
    #include <algorithm>
    #include <cmath>
    #include <cstdio>
    #include <cstring>
    #include <list>
    #include <map>
    #include <iostream>
    #include <queue>
    #include <set>
    #include <stack>
    #include <string>
    #include <vector>
    #include <iomanip>
    #define LL long long
    #define inf 0x3f3f3f3f
    #define INF 0x3f3f3f3f3f3f
    #define PI 3.1415926535898
    #define F first
    #define S second
    #define lson  rt << 1
    #define rson  rt << 1 | 1
    using namespace std;
    
    const int maxn = 57;
    const int maxm = 2e5 + 7;
    int n, m;
    char mp[maxn][maxn];
    int r[maxn][maxn], c[maxn][maxn];
    int cnt = 0, rnum;
    
    vector <int> G[6000];
    int vis[2 * maxn * maxn], link[2 * maxn * maxn];
    bool dfs(int u)
    {
        for (int i = 0; i < G[u].size(); i++)
        {
            int v = G[u][i];
            if (!vis[v])
            {
                vis[v] = 1;
                if (!link[v] || dfs(link[v]))
                {
                    link[v] = u;
                    return true;
                }
            }
        }
        return false;
    }
    int max_match()
    {
        memset(link, 0, sizeof(link));
        int ans = 0;
        for (int i = 1; i <= rnum; i++)
        {
            memset(vis, 0, sizeof(vis));
            if (dfs(i)) ans++;
        }
        return ans;
    }
    int main()
    {
        ios::sync_with_stdio(false);
        cin.tie(0);
        cin >> n >> m;
        for (int i = 1; i <= n; i++)
        {
            for (int j = 1; j <= m; j++)
            {
                cin >> mp[i][j];
                if (mp[i][j] == '*')
                {
                    if (mp[i - 1][j] == '*') r[i][j] = r[i - 1][j];
                    else r[i][j] = ++cnt;
                }
            }
        }
        rnum = cnt;
    
        for (int i = 1; i <= n; i++)
        {
            for (int j = 1; j <= m; j++)
            {
                if (mp[i][j] == '*')
                {
                    if (mp[i][j - 1] == '*') c[i][j] = c[i][j - 1];
                    else c[i][j] = ++cnt;
                    G[r[i][j]].push_back(c[i][j]);
               }
            }
        }
        cout << max_match() << endl;
    
    }
    
     
  • 相关阅读:
    现状和措施
    Nginx http升级到https
    搭建 git 服务器
    Vue + Springboot 开发的简单的用户管理系统
    Vue中的button事件
    Mvc多级Views目录 asp.net mvc4 路由重写及 修改view 的寻找视图的规则
    asp.net mvc 多级目录结构
    Asp.net下使用HttpModule模拟Filter,实现权限控制
    JavaScript事件冒泡简介及应用
    Rhino Mock
  • 原文地址:https://www.cnblogs.com/hfcdyp/p/13374018.html
Copyright © 2011-2022 走看看