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 ...
随机推荐
- winform下的智能提示框
winform下的智能提示框 最近在搞winform的程序,接触到有些可能以后还会用到的功能,所以写到博客园里去,第一可以加深自己的印象,第二可以在以后再遇到同样问题的时候忘记了可以马上回来看看,第三 ...
- jqgrid定义多选操作
先定义: var SelectRowIndx; 然后加入以下属性: multiselect: true, onSelectRow: function () { SelectRowIndx = G ...
- 在 ASP.NET 中使用 jQuery.load() 方法
今天就让我们看看在 ASP.NET 中使用 jQuery.load() 方法来调用 ASP.NET 的方法,实现无刷新的加载数据. 使用 jQuery 的朋友应该知道可以使用 jQuery.load( ...
- 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(31)-MVC使用RDL报表
系列目录 这次我们来演示MVC3怎么显示RDL报表,坑爹的微软把MVC升级到5都木有良好的支持报表,让MVC在某些领域趋于短板 我们只能通过一些方式来使用rdl报表. Razor视图不支持asp.ne ...
- JSON上
关于JSON部分: 目录: 一:定义二:实例三:JSON的语法 一:什么是JSON? 1.JSON:JavaScript 对象表示法(JavaScript Object Notation). 2.JS ...
- 在Outlook中修改IMAP邮件账户的存储位置
使用Outlook收发邮件,但是不想把邮件保存在默认的C盘. 对于POP3账户,Outlook提供了变更数据文件目录的方法,但是IMAP账户无法修改. 网上搜索了很多资料,终于找到一个可行的办法,整理 ...
- Cmap的使用
1.定义 template <class KEY,class ARG_KEY,class VALUE, class ARG_VALUE> class CMap:public CObject ...
- [Python爬虫]cnblogs博客备份工具(可扩展成并行)
并发爬虫小练习. 直接粘贴到本地,命名为.py文件即可运行,运行时的参数为你想要爬取的用户.默认是本博客. 输出是以用户名命名的目录,目录内便是博客内容. 仅供学习python的多线程编程方法,后续会 ...
- JQuery对表格进行排序
添加相关jar <script type="text/javascript" src="jquery-1.1.3.pack.js"></scr ...
- java高薪之路__005_IO流
参考地址: 1. http://blog.csdn.net/yczz/article/details/38761237 File类 ObjectInputStream && Objec ...