【poj2226】Muddy Fields 二分图

本文介绍了一种解决最小点覆盖问题的方法,该问题源于美国计算机奥林匹克竞赛的一个实例。通过建立图模型,利用匈牙利算法求解最大匹配,进而找到覆盖所有泥泞区域所需的最少木板数量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.

Source

USACO 2005 January Gold


把每个连续的行看成左部点,连续的列看成右部点,若它们相交,则连一条边…

现在每条边相当于每个需要覆盖的点,需要选择最少的左部/右部点覆盖所有的边,这是最小点覆盖问题,等于最大匹配

匈牙利打错了的我是什么水平…

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;

const int SZ = 100010;

char maps[1010][1010];

int head[SZ],nxt[SZ],to[SZ],tot = 0;

int x[1010][1010],y[1010][1010],totp = 1;

void build(int f,int t)
{
    to[++ tot] = t;
    nxt[tot] = head[f];
    head[f] = tot;
}

int match[SZ],ans = 0;
bool vis[SZ];
bool dfs(int u)
{
    for(int i = head[u];i;i = nxt[i])
    {
        int v = to[i];
        if(!vis[v])
        {
            vis[v] = 1;
            if(!match[v] || dfs(match[v]))
            {
                match[v] = u;
                return true;
            }
        }
    }
    return false;

}

int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
    for(int i = 1;i <= n;i ++)
        scanf("%s",maps[i] + 1);
    for(int i = 1;i <= n;i ++)
    {
        for(int j = 1;j <= m;j ++)
        {
            if(maps[i][j] == '*')
                x[i][j] = totp;
            else if(maps[i][j - 1] == '*')
                totp ++;
        }
        if(maps[i][m] == '*') totp ++;
    }
    for(int j = 1;j <= m;j ++)
    {
        for(int i = 1;i <= n;i ++)
        {
            if(maps[i][j] == '*')
                y[i][j] = totp;
            else if(maps[i - 1][j] == '*')
                totp ++;
        }
        if(maps[n][j] == '*') totp ++;
    }
    for(int i = 1;i <= n;i ++)
    {
        for(int j = 1;j <= m;j ++)
        {
            if(maps[i][j] == '*')
            {
                build(x[i][j],y[i][j]);
                build(y[i][j],x[i][j]);
            }
        }
    }
/*  for(int i = 1;i <= n;i ++)
    {
        for(int j = 1;j <= m;j ++)
            printf("%d ",x[i][j]);
        puts("");
    }
    for(int i = 1;i <= n;i ++)
    {
        for(int j = 1;j <= m;j ++)
            printf("%d ",y[i][j]);
        puts("");
    }       */
    for(int i = 1;i <= totp;i ++)
    {
        memset(vis,0,sizeof(vis));
        if(dfs(i))
            ans ++;
    }
    printf("%d",ans / 2);
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值