POJ 2226 最小点覆盖(经典建图)
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 8881 | Accepted: 3300 |
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 <iostream>
#include <vector>
#include <queue>
#include <cmath>
#include <set>
using namespace std; #define N 50 int max(int x,int y){return x>y?x:y;}
int min(int x,int y){return x<y?x:y;}
int abs(int x,int y){return x<?-x:x;} int n, m;
vector<int>ve[N*N];
int from[N*N];
bool visited[N*N];
char map[N][N];
int map1[N][N];
int map2[N][N]; int march(int u){
int i, v;
for(i=;i<ve[u].size();i++){
v=ve[u][i];
if(!visited[v]){
visited[v]=true;
if(from[v]==-||march(from[v])){
from[v]=u;
return ;
}
}
}
return ;
} main()
{
int i, j, k;
while(scanf("%d %d",&n,&m)==){
for(i=;i<n;i++) scanf("%s",map[i]);
memset(map1,-,sizeof(map1));
memset(map2,-,sizeof(map2));
int num=;
int maxh=;
//横着处理
for(i=;i<n;i++){
for(j=;j<m;j++){
if(map[i][j]=='*'){
if(j==) map1[i][j]=num;
else{
if(map[i][j-]=='*') map1[i][j]=map1[i][j-];
else map1[i][j]=num;
}
}
else {
if(j<m-&&map[i][j+]=='*') num++;
}
}
num++;
maxh=max(maxh,num);
}
//竖着处理
num=;
for(j=;j<m;j++){
for(i=;i<n;i++){
if(map[i][j]=='*'){
if(i==) map2[i][j]=num;
else{
if(map[i-][j]=='*') map2[i][j]=map2[i-][j];
else map2[i][j]=num;
}
}
else{
if(i<n-&&map[i+][j]=='*') num++;
}
}
num++;
maxh=max(maxh,num);
}
//建二分图
for(i=;i<maxh;i++) ve[i].clear();
for(i=;i<n;i++){
for(j=;j<m;j++){
if(map1[i][j]!=-&&map2[i][j]!=-){
ve[map1[i][j]].push_back(map2[i][j]);
}
}
}
//二分匹配
memset(from,-,sizeof(from));
num=;
for(i=;i<maxh;i++){
memset(visited,false,sizeof(visited));
if(march(i)) num++;
}
printf("%d\n",num);
}
}
POJ 2226 最小点覆盖(经典建图)的更多相关文章
- poj 2226 Muddy Fields(合理建图+二分匹配)
/* 题意:用木板盖住泥泞的地方,不能盖住草.木板任意长!可以重叠覆盖! '*'表示泥泞的地方,'.'表示草! 思路: 首先让我们回忆一下HDU 2119 Matrix这一道题,一个矩阵中只有0, 1 ...
- hdoj--5093--Battle ships(二分图经典建图)
Battle ships Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Tot ...
- poj--1149--PIGS(最大流经典建图)
PIGS Time Limit: 1000MS Memory Limit: 10000KB 64bit IO Format: %I64d & %I64u Submit Status D ...
- poj 1149 PIGS【最大流经典建图】
PIGS Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 18727 Accepted: 8508 Description ...
- HDU 3820 Golden Eggs( 最小割 奇特建图)经典
Golden Eggs Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- POJ 3687 Labeling Balls 逆向建图,拓扑排序
题目链接: http://poj.org/problem?id=3687 要逆向建图,输入的时候要判重边,找入度为0的点的时候要从大到小循环,尽量让编号大的先入栈,输出的时候注意按编号的顺序输出重量, ...
- POJ 2446 最小点覆盖
Chessboard Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 14787 Accepted: 4607 Descr ...
- zoj 3460 Missile【经典建图&&二分】
Missile Time Limit: 2 Seconds Memory Limit: 65536 KB You control N missile launching towers. Ev ...
- hdu 4185 Oil Skimming(二分图匹配 经典建图+匈牙利模板)
Problem Description Thanks to a certain "green" resources company, there is a new profitab ...
随机推荐
- 假如 Micromedia 没被收购,会不会早于 Apple 推动 H5、CSS3 的发展
看着如今大行其道的 H5.CSS3,想想当年的“网页三剑客”,不禁感慨:假如 Micromedia 没被收购,会不会早于 Apple 推动 H5.CSS3 的发展? 当时 Apple 先是询问 Ado ...
- System.IO.File.Create 不会自动释放,一定要Dispose
这样会导致W3P进程一直占用这个文件 System.IO.File.Create(HttpContext.Current.Server.MapPath(strName)) 最好加上Dispose Sy ...
- IBM DB2 For Linux安装指南(转)
一.安装前准备工作: 1.对于Linux系统,需要安装以下软件包: 2.Linux内核设置: 编辑/etc/sysctl.conf文件,加入如下内容: 3.创建相应用户以及组: 官方文档给出必须创建三 ...
- sftp 设置仅能访问自己目录的用户
1. 创建一个目录,owner为root,权限为750或755,此处为 /home/test01 添加一个用户test01,home目录设置为 /home/test01 再创建一个子目录用于用户上传: ...
- [转]NPOI 单元格级别应用
原文地址:http://hi.baidu.com/linrao/item/fadf96dce8770753d63aaef2 HSSFWorkbook hssfworkbook = new HSSFWo ...
- llinux 压缩 解压
1.zip 1) 将文件夹 mydir 压缩为 mydir.zip zip -r mydir.zip mydir 2) 将文件 one.two 压缩到 ot.zip zip -r ot.zip on ...
- C++库汇总
C++库汇总 C++类库介绍再次体现了C++保持核心语言的效率同时大力发展应用库的发展趋势!!在C++中,库的地位是非常高的.C++之父 Bjarne Stroustrup先生多次表示了设计库来扩充功 ...
- php中的匿名函数和闭包(closure)
一:匿名函数 (在php5.3.0 或以上才能使用) php中的匿名函数(Anonymous functions), 也叫闭包函数(closures), 允许指定一个没有名称的函数.最常用的就是回调函 ...
- javascript 去掉空格之后的字符 正则表达式
从后端数据库读取时间时,经常会把整个日期年月日包括时分秒都取到,如2015-1-28 14:56:00,但是一般的我们只需要前面的年月日就行了.一个简单的方法,直接用split(" &quo ...
- 自定义弹出框基于zepto 记得引入zepto
html <!DOCTYPE html> <html> <meta charset="utf-8"> <title></tit ...