POJ2226-Muddy Fields-二分图*
(有任何问题欢迎留言或私聊 && 欢迎交流讨论哦
目录
题意:传送门
原题目描述在最下面。
一个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-二分图*的更多相关文章
- [USACO2005][POJ2226]Muddy Fields(二分图最小点覆盖)
题目:http://poj.org/problem?id=2226 题意:给你一个字符矩阵,每个位置只能有"*"或者“.",连续的横着或者竖的“*"可以用一块木 ...
- POJ2226 Muddy Fields(二分图最小点覆盖集)
题目给张R×C的地图,地图上*表示泥地..表示草地,问最少要几块宽1长任意木板才能盖住所有泥地,木板可以重合但不能盖住草地. 把所有行和列连续的泥地(可以放一块木板铺满的)看作点且行和列连续泥地分别作 ...
- POJ2226 Muddy Fields
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10149 Accepted: 3783 Description Rain ...
- POJ2226 Muddy Fields 二分匹配 最小顶点覆盖 好题
在一个n*m的草地上,.代表草地,*代表水,现在要用宽度为1,长度不限的木板盖住水, 木板可以重叠,但是所有的草地都不能被木板覆盖. 问至少需要的木板数. 这类题的建图方法: 把矩阵作为一个二分图,以 ...
- [POJ] 2226 Muddy Fields(二分图最小点覆盖)
题目地址:http://poj.org/problem?id=2226 二分图的题目关键在于建图.因为“*”的地方只有两种木板覆盖方式:水平或竖直,所以运用这种方式进行二分.首先按行排列,算出每个&q ...
- poj2226 Muddy Fields 填充棒子(二分匹配)
参考博客:https://blog.csdn.net/liujc_/article/details/51287019 参考博客:https://blog.csdn.net/acdreamers/art ...
- poj 2226 Muddy Fields (二分图)
大意:给定n*m网格, 每个格子为泥地或草地, 可以用一些长度任意宽度为1的木板盖住泥地, 要求不能盖到草地, 求最少要多少块木板能盖住所有泥地. 最小点覆盖板子题, 建图跑最大匹配即可. #incl ...
- POJ 2226 Muddy Fields 二分图(难点在于建图)
题意:给定一个矩阵和它的N行M列,其中有一些地方有水,现在有一些长度任意,宽为1的木板,要求在板不跨越草,用一些木板盖住这些有水的地方,问至少需要几块板子? 思路:首先想到如果没有不准跨越草的条件则跟 ...
- poj 2226 Muddy Fields (转化成二分图的最小覆盖)
http://poj.org/problem?id=2226 Muddy Fields Time Limit: 1000MS Memory Limit: 65536K Total Submissi ...
- POJ Muddy Fields 泥泞的牧场 二分图
Muddy Fields Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13235 Accepted: 4879 汪星人 ...
随机推荐
- 【JZOJ6433】【luoguP5664】【CSP-S2019】Emiya 家今天的饭
description analysis 首先可以知道不符合要求的食材仅有一个,于是可以容斥拿总方案数减去选不合法食材的不合法方案数 枚举选取哪一个不合法食材,设\(f[i][j]\)表示到第\(i\ ...
- 11、for和range的用法
// 27_range的使用 package main import ( "fmt" ) func main() { str := "abc" //通过for打 ...
- 每天一个Linux命令:cd(2)
cd cd命令用来切换工作目录至dirname. 其中dirName表示法可为绝对路径或相对路径.若目录名称省略,则变换至使用者的home directory(也就是刚login时所在的目录).另外, ...
- sql delete语句
如果要删除数据库表中的记录,我们可以使用DELETE语句. DELETE语句的基本语法是: DELETE FROM <表名> WHERE ...; 例如,我们想删除students表中id ...
- Shiro学习(5)编码、加密
在涉及到密码存储问题上,应该加密/生成密码摘要存储,而不是存储明文密码.比如之前的600w csdn账号泄露对用户可能造成很大损失,因此应加密/生成不可逆的摘要方式存储. 5.1 编码/解码 Shir ...
- Centos操作命令
查看已经开放的端口:firewall-cmd --list-ports 开启端口:firewall-cmd --zone=public --add-port=80/tcp --permanent 重新 ...
- HDU6333 求组合数前m项的和
目录 分块 莫队 @ HDU6333:传送门 题意:求组合数前m项的和. 在线分块or离线莫队 分块 重要的一个定理: \[C_{n}^{m} = 0\;\;m > n\] \[C_{n}^{m ...
- ubuntu安装mysql 并对外暴露3306端口
安装 sudo apt-get install mysql-client mysql-server vi /etc/mysql/mysql.conf.d/mysqld.cnf bind 127注掉 m ...
- java Logger 类
package org.rx.common; import org.slf4j.LoggerFactory; import java.util.Collections; import java.uti ...
- 记一次面经pm
记一次面经 介绍下你自己的这个项目. 浅谈一下数据结构. 参考网站 在我的记忆中,数据结构包括链表.线性表.栈与队列.数组.一些排序,比如冒泡排序.快速排序.希尔排序.堆排序等,还有一些树 ...