您的位置:首页 > 其它

poj 2226 Muddy Fields(二分图——最小点覆盖)

2014-07-22 22:27 423 查看
Muddy Fields

Time Limit: 1000MSMemory Limit: 65536K
Total Submissions: 7990Accepted: 2949
Description

Rain has pummeled the cows' field, a rectangular grid of R rows and C columns (1 <= R <= 50, 1 <= C <= 50). While good for the grass, the rain makes some patches of bare earth quite muddy. The cows, being meticulous grazers, don't want to get their hooves dirty
while they eat.

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

* Line 1: Two space-separated integers: R and C

* 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

* Line 1: A single integer representing the number of boards FJ needs.

Sample Input
4 4
*.*.
.***
***.
..*.


Sample Output
4


Hint

OUTPUT DETAILS:

Boards 1, 2, 3 and 4 are placed as follows:

1.2.

.333

444.

..2.

Board 2 overlaps boards 3 and 4.

Source

USACO 2005 January Gold

题意:*为泥地,.为平地,用1*inf木板将泥地覆盖,木板无限长,但不能覆盖平地

题解:先横着和竖着处理一次泥地,把连住的泥点看成相同的点,将木板看成点,泥地看成边,一个泥点对应一条边,这样就转化成了二分图,求最小的点将所有的边覆盖即可,最小点覆盖=最大匹配数

直接用HK算法

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <queue>
#define maxm 900008
#define maxn 2508
using namespace std;

struct edge{
int to, next;
}e[maxm];
int nx, ny, m, n;
int tot, head[maxn];
int cx[maxn], cy[maxn];
int distx[maxn], disty[maxn];
char mp[maxn][maxn];
int tp[maxn][maxn];

void addedge(int x, int y){
e[tot].next = head[x];
e[tot].to = y;
head[x] = tot++;
}

void build(){
nx = ny = tot = 1;
memset(head, -1, sizeof(head));
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
if(mp[i][j] == '*'){
if(j > 1 && mp[i][j-1] == '*') tp[i][j] = tp[i][j-1];
else tp[i][j] = nx++;
}
}
}
for(int j = 1; j <= m; j++){
for(int i = 1; i <= n; i++){
if(mp[i][j] == '*'){
if(i > 1 && mp[i-1][j] == '*') addedge(tp[i][j], ny-1);
else addedge(tp[i][j], ny++);
}
}
}
}

int bfs(){
queue<int>q;
int flag = 0;

memset(distx, -1, sizeof(distx));
memset(disty, -1, sizeof(disty));
for(int i = 1; i < nx; i++){
if(cx[i] == -1){
q.push(i);
distx[i] = 0;
}
}
while(!q.empty()){
int x = q.front();
q.pop();
for(int i = head[x]; i != -1; i = e[i].next){
int y = e[i].to;
if(disty[y] == -1){
disty[y] = distx[x]+1;
if(cy[y] == -1) flag = 1;
else{
distx[cy[y]] = disty[y]+1;
q.push(cy[y]);
}
}
}
}
return flag;
}

int dfs(int x){
for(int i = head[x]; i != -1; i = e[i].next){
int y = e[i].to;
if(disty[y] == distx[x]+1){
disty[y] = 0;
if(cy[y] == -1 || dfs(cy[y])){
cx[x] = y;
cy[y] = x;
return 1;
}
}
}
return 0;
}

int graph_match(){
memset(cx, -1, sizeof(cx));
memset(cy, -1, sizeof(cy));
int ret = 0;
while(bfs()){
for(int i = 1; i < nx; i++){
if(cx[i] == -1 && dfs(i)) ret++;
}
}
return ret;
}

int main(){
memset(mp, 0, sizeof(mp));
while(scanf("%d%d", &n, &m) > 0){
for(int i = 1; i <= n; i++){
getchar();
for(int j = 1; j <= m; j++)
scanf("%c", &mp[i][j]);
}
build();
printf("%d\n", graph_match());
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: