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=;
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,,sizeof(set_x));
memset(set_y,,sizeof(set_y));
lenx=;
leny=;
for(int i=;i<n;i++)
{
for(int j=;j<m;j++)
{
int tag=;
if(mz[i][j]=='*')
{
while(j<m&&mz[i][j]=='*')
{
if(!tag)
{
tag=;
lenx++;
}
set_x[i][j]=lenx;
j++;
}
}
}
}
for(int j=;j<m;j++)
{
for(int i=;i<n;i++)
{
int tag=;
if(mz[i][j]=='*')
{
while(i<n&&mz[i][j]=='*')
{
if(!tag)
{
tag=;
leny++;
}
set_y[i][j]=leny;
i++;
}
}
}
} for(int i=;i<n;i++)
{
for(int j=;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=;i<arc[u].size();i++)
{
int to=arc[u][i];
if(!vis[to])
{
vis[to]=;
int w=match[to];
if(w==-||dfs(w))
{
match[to]=u;
match[u]=to;
return true;
}
}
}
return false;
}
int max_flow()
{
int ans=;
memset(match,-,sizeof(match));
for(int i=;i<=lenx;i++)
{
if(match[i]==-)
{
memset(vis,,sizeof(vis));
if(dfs(i)) ans++;
}
}
return ans;
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
for(int i=;i<MAXN;i++) arc[i].clear();
for(int i=;i<n;i++)
{
scanf("%s",mz[i]);
}
build_graph();
int res=max_flow();
printf("%d\n",res);
}
return ;
}

POJ2226(最小顶点覆盖)的更多相关文章

  1. POJ2226 Muddy Fields 二分匹配 最小顶点覆盖 好题

    在一个n*m的草地上,.代表草地,*代表水,现在要用宽度为1,长度不限的木板盖住水, 木板可以重叠,但是所有的草地都不能被木板覆盖. 问至少需要的木板数. 这类题的建图方法: 把矩阵作为一个二分图,以 ...

  2. BZOJ 3140 消毒(最小顶点覆盖)

    题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=3140 题意:最近在生物实验室工作的小T遇到了大麻烦. 由于实验室最近升级的缘故,他的分格 ...

  3. poj 3041 Asteroids (最大匹配最小顶点覆盖——匈牙利模板题)

    http://poj.org/problem?id=3041 Asteroids Time Limit: 1000MS   Memory Limit: 65536K Total Submissions ...

  4. hdoj 1150 Machine Schedule【匈牙利算法+最小顶点覆盖】

    Machine Schedule Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  5. HDU ACM 1054 Strategic Game 二分图最小顶点覆盖?树形DP

    分析:这里使用树形DP做. 1.最小顶点覆盖做法:最小顶点覆盖 == 最大匹配(双向图)/2. 2.树形DP: dp[i][0]表示i为根节点,而且该节点不放,所需的最少的点数. dp[i][1]表示 ...

  6. hdu1054(最小顶点覆盖)

    传送门:Strategic Game 题意:用尽量少的顶点来覆盖所有的边. 分析:最小顶点覆盖裸题,最小顶点覆盖=最大匹配数(双向图)/2. #include <cstdio> #incl ...

  7. hdu 1150 Machine Schedule(最小顶点覆盖)

    pid=1150">Machine Schedule Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/327 ...

  8. poj2594最小顶点覆盖+传递闭包

    传递闭包最开始是在Floyd-Warshall算法里面出现的,当时这算法用的很少就被我忽视了.. 传递闭包是指如果i能到达k,并且k能到达j,那么i就能到达j Have you ever read a ...

  9. hdu1151有向图的最小顶点覆盖

    有向图的最小路径覆盖=V-二分图最大匹配. Consider a town where all the streets are one-way and each street leads from o ...

随机推荐

  1. 《Advanced Bash-scripting Guide》学习(一):对一个增强和广义的删除logfile的脚本的理解

    本文所选的例子来自于<Advanced Bash-scripting Gudie>一书,译者 杨春敏 黄毅 cleanup:一个增强和广义的删除logfile的脚本 #!/bin/bash ...

  2. 通过IndexOf获得DataRow在DataTable中的行号

    Row = dt.Rows.IndexOf(dr);

  3. HDU 1561 The more, The Better(树形DP+01背包)

    The more, The Better Time Limit : 6000/2000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other ...

  4. python基础之函数名称空间与作用域

  5. eclipse配置及常用快捷键

    1. eclipse查看一个方法被谁引用(调用)的快捷键四种方式 1.(首推)双击选中该方法,Ctrl+Alt+H  如果你想知道一个类的方法到底被那些其他的类调用,那么请选中这个方法名,然后按“Ct ...

  6. 【机器学习】Boosting和Bagging的差别

    boosting和bagging的差别: bagging中的模型是强模型,偏差低,方差高.目标是降低方差.在bagging中,每个模型的bias和variance近似相同,但是互相相关性不太高,因此一 ...

  7. delete和truncate区别

    相同之处:truncate在功能上与不带WHERE子句的delete 语句相同:二者均删除表中的全部行.小心使用truncate,删除后就没有了 1.delete : 删除"表格记录&quo ...

  8. access_ok | 检查用户空间内存块是否可用

    access_ok() 函数是用来代替老版本的 verify_area() 函数的.它的作用也是检查用户空间指针是否可用. 函数原型:access_ok (type, addr, size); 变量说 ...

  9. 【android】下载文件至本应用程序的file目录或者sdcard

     一.判断是否有sdcard卡 //判断是否有SD卡 //ture:有SD卡 //false:没有SD卡 public boolean avaiableMedia(){ String status ...

  10. js 柯里化Currying

    今天读一篇博客的时候,看都有关柯里化的东西,由于好奇,特意查了一下,找到一篇比较好的文章,特意收藏. 引子先来看一道小问题:有人在群里出了到一道题目:var s = sum(1)(2)(3) .... ...