poj2226更改行列匹配建图
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 10961 | Accepted: 4071 |
Description
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.
Source
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
const int N=;
char mp[N][N];
int used[N*N],head[N*N],tot,n,m;
struct node
{
int to,next;
} e[];
bool vis[N*N];
int aa[N][N],bb[N][N],a,b;
void add(int u,int v)
{
e[tot].to=v;
e[tot].next=head[u];
head[u]=tot++;
}
bool findx(int u)
{
for(int i=head[u]; ~i; i=e[i].next)
{
int v=e[i].to;
if(vis[v]) continue;
vis[v]=;
if(!used[v]||findx(used[v]))
{
used[v]=u;
return true;
}
}
return false;
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
int maxmatch=;
tot=;
a=b=;
memset(head,-,sizeof(head));
memset(aa,,sizeof(aa));
memset(bb,,sizeof(bb));
for(int i=; i<=n; ++i) scanf("%s",mp[i]+);
for(int i=; i<=n; ++i) for(int j=; j<=m; ++j)
{
bool c=;
while(mp[i][j]=='*'&&j<=m) c=,aa[i][j++]=a;
if(c) ++a;
}
for(int i=; i<=m; ++i) for(int j=; j<=n; ++j)
{
bool c=;
while(mp[j][i]=='*'&&j<=n) c=,bb[j++][i]=b;
if(c) ++b;
}
for(int i=; i<=n; ++i) for(int j=; j<=m; ++j) if(aa[i][j]) add(aa[i][j],bb[i][j]);
memset(used,,sizeof(used));
for(int i=; i<a; ++i)
{
memset(vis,,sizeof(vis));
if(findx(i)) ++maxmatch;
}
printf("%d\n",maxmatch);
}
}
poj2226更改行列匹配建图的更多相关文章
- HDU 3036 Escape 网格图多人逃生 网络流||二分匹配 建图技巧
题意: 每一个' . '有一个姑娘, E是出口,'.'是空地 , 'X' 是墙. 每秒钟每一个姑娘能够走一步(上下左右) 每秒钟每一个出口仅仅能出去一个人 给定n*m的地图, 时限T 问全部姑娘是否能 ...
- TTTTTTTTTTTTTTTTT POJ 2226 草地覆木板 二分匹配 建图
Muddy Fields Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9754 Accepted: 3618 Desc ...
- TTTTTTTTTTTTTTTTTT POJ 2724 奶酪消毒机 二分匹配 建图 比较难想
Purifying Machine Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 5004 Accepted: 1444 ...
- Antenna Placement POJ - 3020 二分图匹配 匈牙利 拆点建图 最小路径覆盖
题意:图没什么用 给出一个地图 地图上有 点 一次可以覆盖2个连续 的点( 左右 或者 上下表示连续)问最少几条边可以使得每个点都被覆盖 最小路径覆盖 最小路径覆盖=|G|-最大匹配数 ...
- POJ-3020 Antenna Placement---二分图匹配&最小路径覆盖&建图
题目链接: https://vjudge.net/problem/POJ-3020 题目大意: 一个n*m的方阵 一个雷达可覆盖两个*,一个*可与四周的一个*被覆盖,一个*可被多个雷达覆盖问至少需要多 ...
- kuangbin带你飞 匹配问题 二分匹配 + 二分图多重匹配 + 二分图最大权匹配 + 一般图匹配带花树
二分匹配:二分图的一些性质 二分图又称作二部图,是图论中的一种特殊模型. 设G=(V,E)是一个无向图,如果顶点V可分割为两个互不相交的子集(A,B),并且图中的每条边(i,j)所关联的两个顶点i和j ...
- HDU 1045 Fire Net(行列匹配变形+缩点建图)
题意:n*n的棋盘上放置房子.同一方同一列不能有两个,除非他们之间被墙隔开,这种话. 把原始图分别按行和列缩点 建图:横竖分区.先看每一列.同一列相连的空地同一时候看成一个点,显然这种区域不可以同一时 ...
- (匹配 二维建图) Antenna Placement --POJ --3020
链接: http://poj.org/problem?id=3020 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82834#probl ...
- Card Game Cheater---hdu1528(扑克建图求二分匹配)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1528 题意就是给有两个人有n张牌第二个人知道第一个人的牌的序列: 然后第二个人尽可能的让自己得更高的分 ...
随机推荐
- 自定义parallelStream的thread pool
目录 简介 通常操作 使用自定义ForkJoinPool 总结 自定义parallelStream的thread pool 简介 之前我们讲到parallelStream的底层使用到了ForkJoin ...
- axios的使用小技巧:如何绕过字符串拼接,直接传递对象
Vue.js官方推荐使用axios作为发送http请求的工具,在使用axios中,有些小技巧是不容易发现的.当我们不知道这些技巧时,我们可能会使用其他"奇技淫巧",比如,我们很容 ...
- SAP WM TO Print Control设置里,Movement Type 的优先级更高
SAP WM TO Print Control设置里,Movement Type 的优先级更高 存储类型的配置: 从storage type GRM 搬到任何地方,都不需要打印TO单. 移动类型的配置 ...
- 基于Swoole的HTTP/HTTPS代理
N行代码实现一个简单的代理服务器 <?php /** * Web代理服务器(支持http/https) * @author zhjx922 */ class WebProxyServer { p ...
- 动态规划经典算法--最长公共子序列 LCS
转移方程 代码: //法一: #include <bits/stdc++.h> using namespace std; //---------------https://lunatic. ...
- CF1328B K-th Beautiful String
CF1328B K-th Beautiful String,然而CF今天却上不去了,这是洛谷的链接 题意 一个长度为\(n\)的字符串,有2个\(\texttt{b}\)和\(n-2\)个\(\tex ...
- JDK 安装及配置环境变量(基于 Linux)
1.先确定虚拟机系统是 32 位还是 64 位 #Linux 指令下输入 getconf LONG_BIT 2.建目录 JDK mkdir JDK 3.通过 rz 导入压缩包 jdk-8u144-li ...
- Nginx编译与安装
我的系统是CentOS-7,Nginx的源码可以在官网下载,网址为:http://nginx.org/en/download.html,我下载了目前的最新版本nginx-1.9.3.tar.gz 下载 ...
- 量子纠错码——Stabilizer codes
对于错误,一般有两种: random: 错误以一定的概率发生在每个比特上(对这种问题的研究一般是信息论中,信道熵一类的问题) worst case: 错误发生在某个比特上,这也是纠错码襄阳解决的问题 ...
- 【漫画】JAVA并发编程三大Bug源头(可见性、原子性、有序性)
原创声明:本文转载自公众号[胖滚猪学编程] 某日,胖滚猪写的代码导致了一个生产bug,奋战到凌晨三点依旧没有解决问题.胖滚熊一看,只用了一个volatile就解决了.并告知胖滚猪,这是并发编程导致的 ...