(有任何问题欢迎留言或私聊 && 欢迎交流讨论哦

目录

题意:传送门

 原题目描述在最下面。

 一个nm的矩阵,有坑有草,可以用1x长度的木板盖住坑,但不能盖到草。问最少用多少木板可以盖掉坑。

思路:

 二分图最小路径覆盖 = N - 最大匹配

  • 把所有同一横排的坑统一标号,同一竖排的坑统一标号。
  • 对于每个坑,有横编号向竖编号连边,然后跑匈牙利算法,当然你也可以跑网络流。

####AC代码:

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
#include<cctype>
#include<string>
#include<cmath>
#define mme(a,b) memset((a),(b),sizeof((a)))
#define fuck(x) cout<<"* "<<x<<"\n"
#define all(x) (x).begin(),(x).end()
#define iis std::ios::sync_with_stdio(false)
using namespace std;
typedef long long LL;
const int INF = 0x3f3f3f3f;
const LL xiao = 0xc0c0c0c0c0c0c0c0;
const int mod = 1e9 + 7;
const double eps = 1e-8;
const int N = 2500+5;
int ab(int x){return x<0?-x:x;}
int n, m;
struct lp{
int v,nex;
}cw[N*N];
int head[N],tot,vis[N],be[N];
char ar[N][N];
int boy[N][N],gril[N][N];
void init(){
mme(head,-1);mme(be,-1);
tot=-1;mme(boy,0);mme(gril,0);
}
void add(int u,int v){
cw[++tot].v=v;cw[tot].nex=head[u];
head[u]=tot;
}
bool dfs(int u){
for(int i=head[u];~i;i=cw[i].nex){
int v = cw[i].v;
if(vis[v])continue;
vis[v]=1;
if(be[v]==-1||dfs(be[v])){
be[v]=u;
return true;
}
}
return false;
}
void hungry(){
int ans=0;
for(int i=1;i<=n;++i){
for(int j=1;j<=n;++j)vis[j]=0;
if(dfs(i))ans++;
}
printf("%d\n", (ans));
}
int main(){
#ifndef ONLINE_JUDGE
freopen("E://ADpan//in.in", "r", stdin);
//freopen("E://ADpan//out.out", "w", stdout);
#endif
while(~scanf("%d%d",&n,&m)){
init();
int n1=0,n2=0;
for(int i=0;i<n;++i){
scanf("%s",ar[i]);
for(int j=0;j<m;++j){
if(ar[i][j]=='*'){
if(j>=1&&ar[i][j-1]=='*'){
boy[i][j] = boy[i][j-1];
}else boy[i][j] = ++n1;
}
}
}
for(int i=0;i<n;++i){
for(int j=0;j<m;++j){
if(ar[i][j]=='*'){
if(i>=1&&ar[i-1][j]=='*'){
gril[i][j] = gril[i-1][j];
}else gril[i][j] = ++n2;
add(boy[i][j],gril[i][j]+n1);
}
}
}
n = n1+n2;
hungry();
}
return 0;
}

最近突然产生了二分图是建单向边还是双向边的疑惑,因为看到这个题建双向边也有过的。仔细看,原来建图和标号是不一样的。有人是把横向编号和竖向标号当成一个部分,有人是当成两个部分。当为两个部分就可以建双向边了,但是似乎没有必要,因为单向边一样可以过,另一部分是没有实际作用的,我在dfs的过程中根本不会搜索到另一部分的点,搜索的全是一边的,如果`u,be[v]`这样都是一边的。

什么时候要建双向边的呢?看题目要求吧,像那些两边点一样的时候,可能要建双向边,比如,似乎hdu2444要双向边才行。

原题目描述:

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

POJ2226-Muddy Fields-二分图*的更多相关文章

  1. [USACO2005][POJ2226]Muddy Fields(二分图最小点覆盖)

    题目:http://poj.org/problem?id=2226 题意:给你一个字符矩阵,每个位置只能有"*"或者“.",连续的横着或者竖的“*"可以用一块木 ...

  2. POJ2226 Muddy Fields(二分图最小点覆盖集)

    题目给张R×C的地图,地图上*表示泥地..表示草地,问最少要几块宽1长任意木板才能盖住所有泥地,木板可以重合但不能盖住草地. 把所有行和列连续的泥地(可以放一块木板铺满的)看作点且行和列连续泥地分别作 ...

  3. POJ2226 Muddy Fields

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10149   Accepted: 3783 Description Rain ...

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

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

  5. [POJ] 2226 Muddy Fields(二分图最小点覆盖)

    题目地址:http://poj.org/problem?id=2226 二分图的题目关键在于建图.因为“*”的地方只有两种木板覆盖方式:水平或竖直,所以运用这种方式进行二分.首先按行排列,算出每个&q ...

  6. poj2226 Muddy Fields 填充棒子(二分匹配)

    参考博客:https://blog.csdn.net/liujc_/article/details/51287019 参考博客:https://blog.csdn.net/acdreamers/art ...

  7. poj 2226 Muddy Fields (二分图)

    大意:给定n*m网格, 每个格子为泥地或草地, 可以用一些长度任意宽度为1的木板盖住泥地, 要求不能盖到草地, 求最少要多少块木板能盖住所有泥地. 最小点覆盖板子题, 建图跑最大匹配即可. #incl ...

  8. POJ 2226 Muddy Fields 二分图(难点在于建图)

    题意:给定一个矩阵和它的N行M列,其中有一些地方有水,现在有一些长度任意,宽为1的木板,要求在板不跨越草,用一些木板盖住这些有水的地方,问至少需要几块板子? 思路:首先想到如果没有不准跨越草的条件则跟 ...

  9. poj 2226 Muddy Fields (转化成二分图的最小覆盖)

    http://poj.org/problem?id=2226 Muddy Fields Time Limit: 1000MS   Memory Limit: 65536K Total Submissi ...

  10. POJ Muddy Fields 泥泞的牧场 二分图

    Muddy Fields Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13235   Accepted: 4879 汪星人 ...

随机推荐

  1. springboot输出hello world,3种方式(String,JSON,jsp),IDEA开发工具

    新建项目: File -> new -> Project -> Spring Initializr -> Next -> Next -> Next-> Pro ...

  2. angulajs 详解 directive 中 的 scope 概念

    Directive 是 angularjs 中最重要的概念,我的理解就是自定义html tag, 这个自定的tag 浏览器不会解析,会有angularjs 来动态解析. 比如在html 中添加 < ...

  3. AutoCAD2016安装破解教程

    AutoCAD2016安装破解教程.本人亲自实验,破解成功,有效.以64位为例. 工具/原料   笔记本电脑 AutoCAD2016安装包 AutoCAD2016注册机(xf-adsk2016_x64 ...

  4. JS基础复习

      js基础语法     Netcape        js基础语法规范(ECMAScript1,2,3,3.1,5(IE9),6   ES    ES6=es2015) DOM BOM       ...

  5. Mentor_丝印检查——手工绘制丝印线条(标注)到丝印位号距离的检查

    http://www.eda365.com/thread-193942-1-1.html 在此之前丝印的检查基本是停留在丝印与阻焊的距离检查,而器件丝印框和手工绘制的线条与器件位号的检查都不到位,据我 ...

  6. Linux内存管理(深入理解Linux内核)

    Linux的内存管理,实际上是借助80x86的硬件分段和分页电路,将逻辑地址转化为物理地址的. 物理内存中,有一部分是一直(Permanently)映射给内核使用的,这部分主要用于保存内核的代码,以及 ...

  7. mysql(自动添加系统时间)timestamp类型字段的CURRENT_TIMESTAMP与ON UPDATE CURRENT_TIMESTAMP属性

    timestamp有两个属性,分别是CURRENT_TIMESTAMP 和ON UPDATE CURRENT_TIMESTAMP两种,使用情况分别如下: 1.CURRENT_TIMESTAMP 当要向 ...

  8. selenium 滑动页面至元素可见

    滚动页面 在自动化操作中,如果web页面过长,而我们需要的元素并不在当前可视页面中,那么selenium就无法对其进行操作:此时,我们就需要像平时操作浏览器一样来滚动页面,使我们需要操作的对象可见! ...

  9. USACO2012 overplanting /// 矩阵切割 递归 oj21547

    题目大意: 在农场的任何一个“轴向对齐”的长方形区域(即垂直和水平方向)种植草坪. 现种植了N(1≤ N ≤10)个不同的矩形区域,其中一些甚至可能重叠. Input Multiple test ca ...

  10. 「SNOI2019」通信 分治优化费用流建图

    题意: n 个排成一列的哨站要进行通信.第 i 个哨站的频段为 ai. 每个哨站 ii 需要选择以下二者之一: 1.直接连接到控制中心,代价为 W:2.连接到前面的某个哨站 j(j<i),代价为 ...