poj 2226 Muddy Fields(水二分图)
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
* 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
Sample Input
4 4
*.*.
.***
***.
..*.
Sample Output
4
Hint
Boards 1, 2, 3 and 4 are placed as follows:
1.2.
.333
444.
..2.
Board 2 overlaps boards 3 and 4.
#include <iostream>
#include <cstdio>
#include <sstream>
#include <cstring>
#include <map>
#include <cctype>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#include <bitset>
#define rap(i, a, n) for(int i=a; i<=n; i++)
#define rep(i, a, n) for(int i=a; i<n; i++)
#define lap(i, a, n) for(int i=n; i>=a; i--)
#define lep(i, a, n) for(int i=n; i>a; i--)
#define rd(a) scanf("%d", &a)
#define rlld(a) scanf("%lld", &a)
#define rc(a) scanf("%c", &a)
#define rs(a) scanf("%s", a)
#define rb(a) scanf("%lf", &a)
#define rf(a) scanf("%f", &a)
#define pd(a) printf("%d\n", a)
#define plld(a) printf("%lld\n", a)
#define pc(a) printf("%c\n", a)
#define ps(a) printf("%s\n", a)
#define MOD 2018
#define LL long long
#define ULL unsigned long long
#define Pair pair<int, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define _ ios_base::sync_with_stdio(0),cin.tie(0)
//freopen("1.txt", "r", stdin);
using namespace std;
const int maxn = , INF = 0x7fffffff;
int n, m; char str[][];
int nu1[][], nu2[][]; int vis[maxn], d[maxn], nex[maxn], head[maxn], cnt, cur[maxn];
int s, t; struct node
{
int u, v, c;
}Node[maxn << ]; void add_(int u, int v, int c)
{
Node[cnt].u = u;
Node[cnt].v = v;
Node[cnt].c = c;
nex[cnt] = head[u];
head[u] = cnt++;
} void add(int u, int v, int c)
{
add_(u, v, c);
add_(v, u, );
} bool bfs()
{
mem(d, );
queue<int> Q;
Q.push(s);
d[s] = ;
while(!Q.empty())
{
int u = Q.front(); Q.pop();
for(int i = head[u]; i != -; i = nex[i])
{
int v = Node[i].v;
if(!d[v] && Node[i].c > )
{
d[v] = d[u] + ;
Q.push(v);
if(v == t) return ;
}
}
}
return d[t] != ;
} int dfs(int u, int cap)
{
int ret = ;
if(u == t || cap == )
return cap;
for(int &i = cur[u]; i != -; i = nex[i])
{
int v = Node[i].v;
if(d[v] == d[u] + && Node[i].c > )
{
int V = dfs(v, min(cap, Node[i].c));
Node[i].c -= V;
Node[i ^ ].c += V;
cap -= V;
ret += V;
if(cap == ) break;
}
}
return ret;
} int Dinic()
{
int ret = ;
while(bfs())
{
memcpy(cur, head, sizeof(head));
ret += dfs(s, INF);
}
return ret;
} int main()
{
cnt = ;
mem(head, -);
rd(n), rd(m);
s = , t = n * m + ;
rep(i, , n)
scanf("%s", str[i]);
int ans = ;
bool flag = ;
rep(i, , n)
{
//int j = 0;
rep(j, , m)
{
if(str[i][j] == '*')
{
if(j == || str[i][j] != str[i][j - ])
ans++; nu1[i][j] = ans;
}
}
} rap(i, , ans)
add(s, i, );
int ss = ans + ;
rep(j, , m)
{
rep(i, , n)
{
if(str[i][j] == '*')
{ if(i == || str[i][j] != str[i - ][j])
ans++;
nu2[i][j] = ans;
}
}
}
rap(i, ss, ans)
add(i, t, );
rep(i, , n)
{ rep(j, , m)
{
if(str[i][j] == '*')
add(nu1[i][j], nu2[i][j], );
}
}
pd(Dinic()); return ;
}
poj 2226 Muddy Fields(水二分图)的更多相关文章
- POJ 2226 Muddy Fields (二分图匹配)
[题目链接] http://poj.org/problem?id=2226 [题目大意] 给出一张图,上面有泥和草地,有泥的地方需要用1*k的木板覆盖, 有草地的地方不希望被覆盖,问在此条件下需要的最 ...
- POJ 2226 Muddy Fields(最小顶点覆盖)
POJ 2226 Muddy Fields 题目链接 题意:给定一个图,要求用纸片去覆盖'*'的位置.纸片能够重叠.可是不能放到'.'的位置,为最少须要几个纸片 思路:二分图匹配求最小点覆盖.和放车那 ...
- poj 2226 Muddy Fields (转化成二分图的最小覆盖)
http://poj.org/problem?id=2226 Muddy Fields Time Limit: 1000MS Memory Limit: 65536K Total Submissi ...
- poj 2226 Muddy Fields(最小覆盖点+构图)
http://poj.org/problem?id=2226 Muddy Fields Time Limit: 1000MS Memory Limit: 65536K Total Submissi ...
- poj 2226 Muddy Fields (二分匹配)
Muddy Fields Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7340 Accepted: 2715 Desc ...
- [POJ] 2226 Muddy Fields(二分图最小点覆盖)
题目地址:http://poj.org/problem?id=2226 二分图的题目关键在于建图.因为“*”的地方只有两种木板覆盖方式:水平或竖直,所以运用这种方式进行二分.首先按行排列,算出每个&q ...
- POJ 2226 Muddy Fields 二分图(难点在于建图)
题意:给定一个矩阵和它的N行M列,其中有一些地方有水,现在有一些长度任意,宽为1的木板,要求在板不跨越草,用一些木板盖住这些有水的地方,问至少需要几块板子? 思路:首先想到如果没有不准跨越草的条件则跟 ...
- poj 2226 Muddy Fields (二分图)
大意:给定n*m网格, 每个格子为泥地或草地, 可以用一些长度任意宽度为1的木板盖住泥地, 要求不能盖到草地, 求最少要多少块木板能盖住所有泥地. 最小点覆盖板子题, 建图跑最大匹配即可. #incl ...
- POJ 2226 Muddy Fields (最小点覆盖集,对比POJ 3041)
题意 给出的是N*M的矩阵,同样是有障碍的格子,要求每次只能消除一行或一列中连续的格子,最少消除多少次可以全部清除. 思路 相当于POJ 3041升级版,不同之处在于这次不能一列一行全部消掉,那些非障 ...
随机推荐
- Centos7 ssh配置RSA证书登录
修改sshd配置文件 vim /etc/ssh/sshd_config #增加以下三项 RSAAuthentication yes PubkeyAuthentication yes Authorize ...
- hibernate 的sum(filed)引发的NullPointException错误解决过程
背景: 在用hql语句进行sum查询时遭遇NPE问题: StringBuilder builder = new StringBuilder("select SUM(actualWorking ...
- [iOS]一行代码集成空白页面占位图(基于runtime+MJRefresh思想)
2018年01月03日阅读 2472 [iOS]一行代码集成空白页面占位图(基于runtime+MJRefresh思想) LYEmptyView 此框架是本人在5,6个月前,公司启动新项目的时候, ...
- JSP 快速入门
目录 生命周期 9大对象 常用指令 基本语法 表达式语言(EL) jstl介绍 常用的jstl标签 生命周期 我们虽然写的是jsp,代码中包含了html.css.js,以及Java代码,但是真正执行的 ...
- tomcat8.0部署启动
http://tomcat.apache.org/download-80.cgi 打开命令行提示符窗口, 进入Tomcat安装目录, 进入bin目录下, 输入:service.bat install ...
- GANs (Generative Adversarial Networks)
#!/usr/bin/python2.7 #coding:utf-8 import tensorflow as tf import numpy as np import matplotlib.pypl ...
- javax.validation.ValidationException: Unable to create a Configuration, because no Bean Validation provider could be found. Add a provider like Hibernate Validator (RI) to your classpath.
项目依赖 <dependency> <groupId>javax</groupId> <artifactId>javaee-api</artifa ...
- Partition算法以及其应用详解上(Golang实现)
最近像在看闲书一样在看一本<啊哈!算法> 当时在amazon上面闲逛挑书,看到巨多人推荐这本算法书,说深入浅出简单易懂便买来阅读.实际上作者描述算法的能力的确令人佩服.就当复习常用算法吧. ...
- python数学第五天【常用概率分布】
1. 概率基本公式 思考题: 3. 两点分布 4. 二项分布 推论一: 5.柏松分布 6. 均匀分布 7. 指数分布 8. 正态分布 9.常见分布的总结
- 思维导图,UML图,程序流程图制作从入门到精通
工具: https://www.processon.com/ 第一 用例图 第二 时序图 第三 流程图