poj 2226 Muddy Fields(水二分图)

本文介绍了一种解决农场泥泞区域覆盖问题的算法,通过使用Dinic算法找到最少数量的木板来覆盖所有泥泞区域,同时确保木板不覆盖草地。算法将连续的泥泞区域缩点并构建二分图进行匹配。
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.
 
虽然想到了做法 但没想到和这题的联系 细想才明白过来
一个提示
 
只要涉及这种类型的题 一套Dinic就完事了、
 
每一行连续的点缩为一个点 放在左边
每一列连续的点缩为一个点 放在右边
匹配就好了
这里用的Dinic  匈牙利和hc也行
#include <iostream>
#include <cstdio>
#include <sstream>
#include <cstring>
#include <map>
#include <cctype>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#include <bitset>
#define rap(i, a, n) for(int i=a; i<=n; i++)
#define rep(i, a, n) for(int i=a; i<n; i++)
#define lap(i, a, n) for(int i=n; i>=a; i--)
#define lep(i, a, n) for(int i=n; i>a; i--)
#define rd(a) scanf("%d", &a)
#define rlld(a) scanf("%lld", &a)
#define rc(a) scanf("%c", &a)
#define rs(a) scanf("%s", a)
#define rb(a) scanf("%lf", &a)
#define rf(a) scanf("%f", &a)
#define pd(a) printf("%d\n", a)
#define plld(a) printf("%lld\n", a)
#define pc(a) printf("%c\n", a)
#define ps(a) printf("%s\n", a)
#define MOD 2018
#define LL long long
#define ULL unsigned long long
#define Pair pair<int, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define _  ios_base::sync_with_stdio(0),cin.tie(0)
//freopen("1.txt", "r", stdin);
using namespace std;
const int maxn = 10000, INF = 0x7fffffff;
int n, m;

char str[55][55];
int nu1[55][55], nu2[55][55];

int vis[maxn], d[maxn], nex[maxn], head[maxn], cnt, cur[maxn];
int s, t;

struct node
{
    int u, v, c;
}Node[maxn << 1];

void add_(int u, int v, int c)
{
    Node[cnt].u = u;
    Node[cnt].v = v;
    Node[cnt].c = c;
    nex[cnt] = head[u];
    head[u] = cnt++;
}

void add(int u, int v, int c)
{
    add_(u, v, c);
    add_(v, u, 0);
}

bool bfs()
{
    mem(d, 0);
    queue<int> Q;
    Q.push(s);
    d[s] = 1;
    while(!Q.empty())
    {
        int u = Q.front(); Q.pop();
        for(int i = head[u]; i != -1; i = nex[i])
        {
            int v = Node[i].v;
            if(!d[v] && Node[i].c > 0)
            {
                d[v] = d[u] + 1;
                Q.push(v);
                if(v == t) return 1;
            }
        }
    }
    return d[t] != 0;
}

int dfs(int u, int cap)
{
    int ret = 0;
    if(u == t || cap == 0)
        return cap;
    for(int &i = cur[u]; i != -1; i = nex[i])
    {
        int v = Node[i].v;
        if(d[v] == d[u] + 1 && Node[i].c > 0)
        {
            int V = dfs(v, min(cap, Node[i].c));
            Node[i].c -= V;
            Node[i ^ 1].c += V;
            cap -= V;
            ret += V;
            if(cap == 0) break;
        }
    }
    return ret;
}

int Dinic()
{
    int ret = 0;
    while(bfs())
    {
        memcpy(cur, head, sizeof(head));
        ret += dfs(s, INF);
    }
    return ret;
}


int main()
{
    cnt = 0;
    mem(head, -1);
    rd(n), rd(m);
    s = 0, t = n * m + 1;
    rep(i, 0, n)
        scanf("%s", str[i]);
    int ans = 0;
    bool flag = 0;
    rep(i, 0, n)
    {
        //int j = 0;
        rep(j, 0, m)
        {
            if(str[i][j] == '*')
            {
                if(j == 0 || str[i][j] != str[i][j - 1])
                    ans++;

                nu1[i][j] = ans;
            }
        }
    }

    rap(i, 1, ans)
        add(s, i, 1);
    int ss = ans + 1;
    rep(j, 0, m)
    {
        rep(i, 0, n)
        {
            if(str[i][j] == '*')
            {

                if(i == 0 || str[i][j] != str[i - 1][j])
                    ans++;
                nu2[i][j] = ans;
            }
        }
    }
    rap(i, ss, ans)
        add(i, t, 1);
    rep(i, 0, n)
    {

        rep(j, 0, m)
        {
            if(str[i][j] == '*')
                add(nu1[i][j], nu2[i][j], 1);
        }
    }
    pd(Dinic());


    return 0;
}

 

 
 

转载于:https://www.cnblogs.com/WTSRUVF/p/10694039.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值