poj 2226-二分图的最小顶点覆盖

来源:互联网 发布:软件广告收费模式 编辑:程序博客网 时间:2024/05/16 00:36

题目意思:牛要回家,路上遇到泥泞的地方需要主人铺上木板,求出主人至少要铺多少木板???

其中'*'表示泥泞,‘.’表示草坪


Muddy Fields
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 6967 Accepted: 2578

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



代码:

#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <algorithm>using namespace std;const int MAX = 55;const int MAXN = 1005;int n, m, k1, k2;int g[MAXN][MAXN];int vis[MAXN], yM[MAXN];char map[MAX][MAX];int mark1[MAX][MAX];int mark2[MAX][MAX];int Search(int u){int v;for( v = 0; v < k2; v ++) {if(g[u][v] && !vis[v]) {vis[v] = 1;if(yM[v] == -1 || Search(yM[v])) {yM[v] = u;return 1;}}}return 0;}int MaxMatch() {int u,ret = 0;memset(yM, -1, sizeof(yM));for(u = 0; u < k1; u ++) {memset(vis, 0, sizeof(vis));if(Search(u))ret ++;}return ret;}void solve() {k1 = 0;for(int i = 0; i < n; i ++)for(int j = 0; j < m; j ++) {if(map[i][j] == '*') {while(map[i][j] == '*') {mark1[i][j] = k1;j ++;}k1 ++;}}k2 = 0;for(int i = 0; i < m; i ++)for(int j = 0; j < n; j ++){if(map[j][i] == '*') {while(map[j][i] == '*') {mark2[j][i] = k2;j ++;}k2 ++;}}for(int i = 0; i < n; i ++)for(int j = 0; j < m; j ++) {if(map[i][j] == '*') {g[mark1[i][j]][mark2[i][j]] = 1;}}}int main() {int ans;while(scanf("%d%d", &n, &m) != EOF){memset(g, 0, sizeof(g));memset(mark1, 0, sizeof(mark1));memset(mark2, 0, sizeof(mark2));for(int i = 0; i < n; i ++)scanf("%s", map[i]);solve();ans = MaxMatch();printf("%d\n", ans);}return 0;}


原创粉丝点击