POJ2226 Muddy Fields 二分匹配 最小顶点覆盖 好题
在一个n*m的草地上,.代表草地,*代表水,现在要用宽度为1,长度不限的木板盖住水,
木板可以重叠,但是所有的草地都不能被木板覆盖。
问至少需要的木板数。
这类题的建图方法:
把矩阵作为一个二分图,以行列分别作为2个顶点集
首先以每一行来看,把这一行里面连续的*编号,作为一个顶点
再以每一列来看,把这一列里面连续的*编号,作为一个顶点
则每一个*都有2个编号,以行看时有一个,以列看时有一个,则把这2个编号连边,容量为1
再建一个源点,连接所有行的编号,一个汇点,连接所有列的编号
这道题要求的是,所有*都被覆盖,即找到一个顶点的集合S,使得任意边都有至少一个顶点属于
S,即求一个点集顶点覆盖S,又要木板数最少,所以求的就是最小顶点覆盖。
最小顶点覆盖怎么求?
二分图中,有:
最小顶点覆盖=最大匹配
所以这道题就转化为求二分图的最大匹配了
再转化为最大流dinic算法。
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue> using namespace std; const int maxn=;
const int inf=0x3f3f3f3f;
const int s=;
int t;
int tota;
int totb; inline int min(int x,int y)
{
return x<y?x:y;
} struct Edge
{
int to,cap,rev;
};
vector<Edge>edge[maxn];
int iter[maxn];
int level[maxn];
char str[][];
int hash[][]; void addedge(int from,int to,int cap)
{
edge[from].push_back((Edge){to,cap,edge[to].size()});
edge[to].push_back((Edge){from,,edge[from].size()-});
} void build_graph(int n,int m)
{
for(int i=;i<n*m;i++)
edge[i].clear();
tota=;
for(int i=;i<=n;i++)
{
int j=;
while(j<=m)
{
if(str[i][j]=='*')
{
tota++;
hash[i][j]=tota;
while(j<=m&&str[i][j+]=='*')
{
j++;
hash[i][j]=tota;
}
}
j++;
}
}
totb=tota;
for(int j=;j<=m;j++)
{
int i=;
while(i<=n)
{
if(str[i][j]=='*')
{
totb++;
addedge(hash[i][j],totb,);
while(i<=n&&str[i+][j]=='*')
{
i++;
addedge(hash[i][j],totb,);
}
}
i++;
}
}
t=tota+totb+;
for(int i=;i<=tota;i++)
addedge(s,i,);
for(int i=tota+;i<=totb;i++)
addedge(i,t,);
} void bfs()
{
memset(level,-,sizeof level);
queue<int>que;
while(!que.empty())
que.pop();
que.push(s);
level[s]=;
while(!que.empty())
{
int u=que.front();
que.pop();
for(int i=;i<edge[u].size();i++)
{
Edge &e=edge[u][i];
if(e.cap>&&level[e.to]<)
{
level[e.to]=level[u]+;
que.push(e.to);
}
}
}
} int dfs(int u,int f)
{
if(u==t)
return f;
for(int &i=iter[u];i<edge[u].size();i++)
{
Edge &e=edge[u][i];
if(e.cap>&&level[e.to]>level[u])
{
int d=dfs(e.to,min(f,e.cap));
if(d>)
{
e.cap-=d;
edge[e.to][e.rev].cap+=d;
return d;
} }
}
return ;
} int solve()
{
int flow=;
while(true)
{
bfs();
if(level[t]<)
return flow;
memset(iter,,sizeof iter);
int f;
while(f=dfs(s,inf))
{
flow+=f;
}
}
} int main()
{
int n,m;
while(~scanf("%d%d",&n,&m))
{
for(int i=;i<=n;i++)
{
scanf("%s",str[i]+);
}
build_graph(n,m);
printf("%d\n",solve());
}
return ;
}
POJ2226 Muddy Fields 二分匹配 最小顶点覆盖 好题的更多相关文章
- poj2226-Muddy Fields二分匹配 最小顶点覆盖 好题
题目 给到一个矩阵,有些格子上是草,有些是水.需要用宽度为1,长度任意的若干块木板覆盖所有的水,并不能覆盖草,木板可以交叉,但只能横竖放置,问最少要多少块板. 分析 经典的矩阵二分图构图和最小点覆盖. ...
- poj 2226 Muddy Fields (二分匹配)
Muddy Fields Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7340 Accepted: 2715 Desc ...
- POJ 1498[二分匹配——最小顶点覆盖]
题目链接:[http://acm.hdu.edu.cn/showproblem.php?pid=1498] 题意:给出一个大小为n*n(0<n<100)的矩阵,矩阵中放入m种颜色(标号为1 ...
- [USACO2005][POJ2226]Muddy Fields(二分图最小点覆盖)
题目:http://poj.org/problem?id=2226 题意:给你一个字符矩阵,每个位置只能有"*"或者“.",连续的横着或者竖的“*"可以用一块木 ...
- POJ2226 Muddy Fields(二分图最小点覆盖集)
题目给张R×C的地图,地图上*表示泥地..表示草地,问最少要几块宽1长任意木板才能盖住所有泥地,木板可以重合但不能盖住草地. 把所有行和列连续的泥地(可以放一块木板铺满的)看作点且行和列连续泥地分别作 ...
- nyoj 237 游戏高手的烦恼 二分匹配--最小点覆盖
题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=237 二分匹配--最小点覆盖模板题 Tips:用邻接矩阵超时,用数组模拟邻接表WA,暂时只 ...
- poj2226 Muddy Fields 填充棒子(二分匹配)
参考博客:https://blog.csdn.net/liujc_/article/details/51287019 参考博客:https://blog.csdn.net/acdreamers/art ...
- UVA 11419 SAM I AM(最大二分匹配&最小点覆盖:König定理)
题意:在方格图上打小怪,每次可以清除一整行或一整列的小怪,问最少的步数是多少,又应该在哪些位置操作(对输出顺序没有要求). 分析:最小覆盖问题 这是一种在方格图上建立的模型:令S集表示“行”,T集表示 ...
- HDU 3861 The King’s Problem(tarjan缩点+最小路径覆盖:sig-最大二分匹配数,经典题)
The King’s Problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
随机推荐
- Git连接Github
环境:Ubuntu Server 12.04 安装Git apt-get install git git-core 配置本机Git git config --global user.name &quo ...
- 附录二 C语言标准库
上章回顾 数组和指针相同与不同 通过指针访问数组和通过数组访问指针 指针在什么时候可以加减运算 函数指针的申明和调用 函数数组和数组函数 git@github.com:Kevin-Dfg/Data-S ...
- phpstrom 编辑器设置
http://www.jb51.net/article/58069.htm 配置sublime主题 保存配置的路径为:C:\Users\Administrator\.WebIde100\config ...
- asm单机dg dbca报错ORA-01031 CRS-2676,rman restore主库控制文件报错ORA-15081
dg-> ll $ORACLE_HOME/bin/oracle -r-xr-s--x 1 oracle asmadmin 210824714 Nov 20 16:41 /u01/app/orac ...
- Eclipse+Maven创建webapp项目<一><二><三>
转-http://www.cnblogs.com/candle806/p/3439469.html Eclipse+Maven创建webapp项目<一> 1.开启eclipse,右键new ...
- 【转】Python numpy库的nonzero函数用法
当使用布尔数组直接作为下标对象或者元组下标对象中有布尔数组时,都相当于用nonzero()将布尔数组转换成一组整数数组,然后使用整数数组进行下标运算. nonzeros(a) 返回数组a中值不为零的元 ...
- Linux 数据流重定向
1.三种数据流重定向1)标准输入(stdin):代码为0,使用0<或0<<,其中代码0可以省略2)标准输出(stdout):代码为1,使用1>或1>>,其中代码1可 ...
- css @语法,@规则 @import @charset @font-face @fontdef @media @page
CSS At-Rules Reference 样式表规则 At-Rules 样式表规则 CSS Version 版本 Compatibility 兼容性 Description 简介 @impo ...
- 010. 使用.net框架提供的属性
C#允许在类和类成员上声明特性(类), 可在运行时解释类和类的成员. 这个特性也称为属性, 使用Attribute.下面演示如何使用.net框架提供的属性. using System; using S ...
- linux服务之svn
架构:c/s 开发语言:python 服务器端:在linux平台下部署 客户端:分gui与cli两种操作界面 相关包: http://blog.sina.com.cn/s/blog_53b95aec0 ...