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 汪星人 ...
随机推荐
- 分布式项目中增加品牌前端页面出现Uncaught Error: [$injector:modulerr] bug后的原因以及改正方式
分布式查询品牌页面时 controller路径正确访问,比如输入 http://localhost:8081/brand/findPage.do?page=3&rows=6 是可以正常显示数据 ...
- javascript基础总结之实例(二)
div的显示和隐藏 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://w ...
- 详解Windows注册表分析取证
大多数都知道windows系统中有个叫注册表的东西,但却很少有人会去深入的了解它的作用以及如何对它进行操作.然而对于计算机取证人员来说注册表无疑是块巨大的宝藏.通过注册表取证人员能分析出系统发生了什么 ...
- Ubuntu 16.04系统上修改Docker镜像的存储路径 (转)
转自:https://blog.csdn.net/qihongchao/article/details/80651492 dba专门挂了一个硬盘让我放项目(测试环境)因为系统空间比较小,希望把dock ...
- 记Selenium HTMLTestRunner 无法生成测试报告的总结
使用Python ,HTMLTestRunner 生成测试报告时,遇到很奇怪的问题,明明运行的结果,没有任何报错,就是不生成测试报告,纠结好久.google+baidu搜索结果也不满意,最后终于解 ...
- Codeforces451A-Game With Sticks-思维
After winning gold and silver in IOI 2014, Akshat and Malvika want to have some fun. Now they are pl ...
- 剑指offer——53字符流中第一个只出现一次的字符
题目描述 请实现一个函数用来找出字符流中第一个只出现一次的字符.例如,当从字符流中只读出前两个字符"go"时,第一个只出现一次的字符是"g".当从该字符流中读出 ...
- Firefox好用的快捷键
1,Alt+D 你可以使用该快捷键直接把光标转到火狐的地址栏.非常有用 2,Ctrl + T和Ctrl + Shift + T Ctrl+T帮你打开一个新标签,Ctrl+Shift+T重新打开上次关闭 ...
- linux模范配置文件格式
模范配置文件 #--------------------------------------------------------------------- # Global settings #--- ...
- Hadoop 基础知识
Hadoop 数据是存储在HDFS, Mapreduce 是一种计算框架,负责计算处理. HDFS上的数据存储默认是本地节点数据一份,同一机架不同节点一份,不同机架不同节点一份.默认是存储3份 HDF ...