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.
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.
* 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;}
- poj 2226-二分图的最小顶点覆盖
- poj-3041【二分图最小顶点覆盖】
- poj 2226 二分图 最小顶点覆盖 “草泥马”
- poj 1325 Machine Schedule---二分图求最小顶点覆盖
- POJ 3020Antenna Placement(二分图最小顶点覆盖)
- POJ 3041 (二分图匹配 最小顶点覆盖)
- poj 3041 Asteroids( 最小顶点覆盖,二分图)
- POJ 3041-Asteroids(二分图最小顶点覆盖)
- 【最小顶点覆盖】POJ 2226
- Asteroids----------求二分图的最小顶点覆盖问题
- hdu1150 Machine Schedule (二分图的最小顶点覆盖)
- poj3041_Asteroids_二分图最小顶点覆盖
- hdu 1150二分图最小顶点覆盖
- poj1325_二分图最小顶点覆盖集
- hud1150二分图最小顶点覆盖
- poj3041 二分图最小顶点覆盖
- 二分图最小顶点覆盖 hdu1150
- hdu1150(二分图最小顶点覆盖)
- Meteor Error: Must pass options.rootUrl or set ROOT_URL in the server environment解决
- 黑马程序员_HTML表单标签
- html5视频方案
- GTK+Glade3 Gtk-WARNING **: Could not find signal handler 问题最终解析
- 对I2C总线时序的一点理解以及ACK和NACK(NAK)
- poj 2226-二分图的最小顶点覆盖
- 面试题
- JSP的EL表达式说明
- Ubuntu11.10 更新源source.list
- 有关platform builder 5.0编写流接口驱动程序
- 风险评估系统OpenVAS配置使用教程说明
- 在linux下查看CPU,内存大小
- Java 中的比较 equals() hashcode() comparable
- 如何在NP下读写游戏内存及如何进入NP进程