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. AOP切面用于系统日志

    import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.*; import org.springframework. ...

  2. java XML解析

    package com.kpsh.myself; import java.io.File;import java.io.FileInputStream;import java.util.List; i ...

  3. scala学习手记18 - Any和Nothing

    Any 前面已经有两次提到过:在scala中,Any类是所有类的超类. Any有两个子类:AnyVal和AnyRef.对应Java直接类型的scala封装类,如Int.Double等,AnyVal是它 ...

  4. JSP导入包

    1. <%@page import="java.util.Date, mianBao.*, z_utils.*"%> 2. 3.

  5. 深入浅出TensorFlow(二):TensorFlow解决MNIST问题入门

    2017年2月16日,Google正式对外发布Google TensorFlow 1.0版本,并保证本次的发布版本API接口完全满足生产环境稳定性要求.这是TensorFlow的一个重要里程碑,标志着 ...

  6. JAVAWEB常见易错问题大汇总

    1.如何修改tomcat访问端口? Answer: conf/server.xml 2.如何配置tomcat Manager访问用户? Answer: conf/tomcat-users.xml 主要 ...

  7. LeetCode第[3]题(Java):Longest Substring Without Repeating Characters 标签:Linked List

    题目中文:没有重复字符的最长子串 题目难度:Medium 题目内容: Given a string, find the length of the longest substring without ...

  8. 手把手教你用Vue2+webpack+node开发一个H5 app

    手把手教你用Vue2+webpack+node开发一个H5 app ​前一篇vue2 + webpack + node 开发一个小demo说到了用vue的一些基本用法,这一篇就讲一个复杂一点的更完整的 ...

  9. 【C#笔札】1 string类型

    C中没有string这个类型,而是用字符串数组来实现,相对来说比较麻烦. LABVIEW相对来说要简单太多,毕竟他主要的精力集中在硬件控制上,软件操作方面极其简单. C#类似,C#中有System.S ...

  10. 通用 mysql配置

    windows:my.ini [mysqld] # Remove leading # and set to the amount of RAM for the most important data ...