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

目录

题意:传送门

 原题目描述在最下面。

 一个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. Android中的ListView的绘制过程中执行的方法

    首先,系统在绘制ListView之前, 将会先调用getCount方法来获取Item的个数.(如果getCount方法返回0的话,列表时不显示任何内容的) 之后每绘制一个 Item就会调用一次getV ...

  2. NX二次开发-Block UI C++界面Specify Point(指定点)控件的获取(持续补充)

    Specify Point(指定点)控件的获取 NX9+VS2012 #include <uf.h> #include <uf_ui.h> UF_initialize(); / ...

  3. js设计模式——6.模板方法模式与职责链模式

    js设计模式——6.模板方法模式与职责链模式 职责链模式

  4. Delphi多线程详解

    (整理自网络) Delphi多线程处理 1-1多线程的基本概念 WIN 98/NT/2000/XP 是个多任务操作系统,也就是:一个进程可以划分为多个线程,每个线程轮流占用CPU 运行时间和资源,或者 ...

  5. JS基础复习

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

  6. Linux关闭端口

    1.  查看哪些端口被占用 $ netstat -anp | grep 2042 tcp 0 0 192.168.56.1:2042 0.0.0.0:* LISTEN 8974/python 2. 删 ...

  7. 2019杭电多校第一场hdu6579 Operation(线性基)

    Operation 题目传送门 解题思路 把右边的数尽量往高位放,构造线性基的时候同时记录其在原序列中的位置,在可以插入的时候如果那个位置上存在的数字的位置比新放入的要小,就把旧的往后挤.用这种发现构 ...

  8. SingalR 构建 推送服务器初探

    项目需要用到推送,于是重新研究了下推送框架,最好能够独立成一个服务,与业务无关的服务,可以给所有的项目通用.找了好久最终决定用SinglR 框架. Signal 是微软支持的一个运行在 Dot NET ...

  9. add new color number to the color drop down in enovia PLM

    Ticket description:A55482L Ticket attachment: Open the attached page 2. Open the internationalizing ...

  10. 关于ajax请求status 200 却进入error 回调函数或显示跨域问题的解决方案及原因

    这虽然不是前端的问题吧,但如果遇到那种不靠谱的后台 还是可以拿来打脸的 转:https://segmentfault.com/a/1190000012469713