码迷,mamicode.com
首页 > 其他好文 > 详细

[POJ2226]Muddy Fields(二分图匹配)

时间:2020-07-24 22:06:12      阅读:92      评论:0      收藏:0      [点我收藏+]

标签:max   names   may   pat   include   ons   oar   boa   put   

【原题】

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;

}

 

 

[POJ2226]Muddy Fields(二分图匹配)

标签:max   names   may   pat   include   ons   oar   boa   put   

原文地址:https://www.cnblogs.com/hfcdyp/p/13374018.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!