[POJ 2226] Muddy Fields【二分图最小点覆盖】

  • 2018-01-20
  • 0
  • 0

Problem:

Time Limit: 1000MS Memory Limit: 65536K

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

Solution:

本题构造二分图有一定难度。

可以将横向的条形泥泞区依次编号,同理将纵向的也依次编号。样例的标号如下(0 表示 10):

1.2.   6.9.
.333   .890
444.   789.
..5.   ..9.

从每格泥泞区的横向编号向纵向编号连边,表示至少需要取该边的一个顶点,问题就转化成了求二分图的最小点覆盖

【Kőnig 定理】(Wikipedia)

  • 二分图的最小点覆盖数 = 二分图的最大匹配数

即只需要求该二分图的最大匹配数即可。

我用了两种算法实现二分图最大匹配的求解,一种是 O(VE) 的匈牙利算法(见 Code #1),另一种是其改进版 O(√VE) 的 Hopcroft-Karp 算法(见 Code #2),但是在本题中后者由于常数较大?跑的比前者还慢。。

Hopcroft-Karp 算法的 Wikipedia

Code #1: O(VE) [692K, 16MS]

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;

int R, C;
char mp[55][55];
int label[55][55], curlabel = 0, lcnt;
int match[2505];
bool vis[2505];

struct Edge{
	int np;
	Edge *nxt;
};

struct Graph{
	Edge *V[2505], E[2505];
	int tope;
	
	inline void addedge(int u, int v) {E[++tope].np = v, E[tope].nxt = V[u], V[u] = &E[tope];}
} G;

inline bool Augmentable(int u){
	for(register Edge *ne = G.V[u]; ne; ne = ne->nxt){
		if(vis[ne->np]) continue;
		vis[ne->np] = 1;
		if(!match[ne->np] || Augmentable(match[ne->np])){
			match[ne->np] = u;
			return 1;
		}
	}
	return 0;
}

inline int Hungarian(){
	int maxmatch = 0;
	memset(match, 0, sizeof(match));
	for(register int i = 1; i <= lcnt; i++){
		memset(vis, 0, sizeof(vis));
		if(Augmentable(i)) maxmatch++;
	}
	return maxmatch;
}

int main(){
	scanf("%d%d", &R, &C);
	for(register int i = 1; i <= R; i++) scanf("%s", mp[i] + 1);
	for(register int i = 1; i <= R; i++)
		for(register int j = 1; j <= C; j++){
			if(mp[i][j] != '*') continue;
			if(mp[i][j - 1] != '*') curlabel++;
			label[i][j] = curlabel;
		}
	lcnt = curlabel;
	for(register int i = 1; i <= C; i++)
		for(register int j = 1; j <= R; j++){
			if(mp[j][i] != '*') continue;
			if(mp[j - 1][i] != '*') curlabel++;
			G.addedge(label[j][i], curlabel);
		}
	int minVcover = Hungarian();
	printf("%d\n", minVcover);
	return 0;
}

Code #2: O(√VE) [724K, 63MS]

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;

int R, C;
char mp[55][55];
int label[55][55], curlabel = 0, lcnt;
int lm[2505], rm[2505];

struct Edge{
	int np;
	Edge *nxt;
};

struct Graph{
	Edge *V[2505], E[2505];
	int tope;
	
	inline void addedge(int u, int v) {E[++tope].np = v, E[tope].nxt = V[u], V[u] = &E[tope];}
} G;

int lev[2505];
bool vis[2505];
int q[2505], fr, re;

inline bool HK_BFS(){
	memset(lev, -1, sizeof(lev));
	fr = re = 0;
	for(register int i = 1; i <= lcnt; i++)
		if(!lm[i]) q[re++] = i, lev[i] = 0;
	while(fr != re){
		int u = q[fr++];
		if(lev[u] == -1) continue;
		for(register Edge *ne = G.V[u]; ne; ne = ne->nxt)
			if(lev[rm[ne->np]] == -1)
				lev[rm[ne->np]] = lev[u] + 1, q[re++] = rm[ne->np];
	}
	return lev[0] != -1;  // The Augmenting Path ends at a null vertex 0
}

inline int HK_DFS(int u){
	if(!u) return 1;
	for(register Edge *ne = G.V[u]; ne; ne = ne->nxt)
		if(lev[rm[ne->np]] == lev[u] + 1 && HK_DFS(rm[ne->np])){
			rm[ne->np] = u, lm[u] = ne->np;
			return 1;
		}
	lev[u] = -1;  // Fix this vertex when no more Augmenting Paths go across it
	return 0;
}

inline int Hopcroft_Karp(){
	int maxmatch = 0;
	memset(lm, 0, sizeof(lm)), memset(rm, 0, sizeof(rm));
	while(HK_BFS()){
		memset(vis, 0, sizeof(vis));
		for(register int i = 1; i <= lcnt; i++)
			if(!lm[i] && HK_DFS(i)) maxmatch++;
	}
	return maxmatch;
}

int main(){
	scanf("%d%d", &R, &C);
	for(register int i = 1; i <= R; i++) scanf("%s", mp[i] + 1);
	for(register int i = 1; i <= R; i++)
		for(register int j = 1; j <= C; j++){
			if(mp[i][j] != '*') continue;
			if(mp[i][j - 1] != '*') curlabel++;
			label[i][j] = curlabel;
		}
	lcnt = curlabel;
	for(register int i = 1; i <= C; i++)
		for(register int j = 1; j <= R; j++){
			if(mp[j][i] != '*') continue;
			if(mp[j - 1][i] != '*') curlabel++;
			G.addedge(label[j][i], curlabel);
		}
	int minVcover = Hopcroft_Karp();
	printf("%d\n", minVcover);
	return 0;
}

评论

还没有任何评论,你来说两句吧



新博客地址: darkleafin.cf

常年不在线的QQ:
49750

不定期更新的GitHub:
https://github.com/Darkleafin


OPEN AT 2017.12.10

如遇到代码不能正常显示的情况,请刷新页面。
Please refresh the page if the code cannot be displayed normally.


发现一个优美的网站:
https://visualgo.net/en
















- Theme by Qzhai