【二分图】poj 2226 Muddy Fields

来源:互联网 发布:mfc udp编程实例 编辑:程序博客网 时间:2024/05/23 14:44
Muddy Fields
Time Limit: 1000MS
Memory Limit: 65536KTotal Submissions: 10554
Accepted: 3925

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.



每一个横向或纵向的*区域都可以看做是一个联通块那么只要把横向的连通块放在左集合,把纵向的连通块放在右集合 把点看左边那么只要所有的边被覆盖则就是覆盖掉所有的*了这样想来因为左右集合放的就是所求的连通块,那么只要求出最小点覆盖集即ok啦最小点覆盖集 = 二分图最大匹配///AC代码
#include <algorithm>#include <cmath>#include <cstdio>#include <cstring>#include <ctime>#include <iostream>#include <map>#include <queue>#include <set>#include <stack>#include <string>#include <vector>#define eps 1e-8#define INF 0x7fffffff#define maxn 100005#define PI acos(-1.0)using namespace std;typedef long long LL;const int N = 302;/*变种1:二分图的最小顶点覆盖:假如选了一个点就相当于覆盖了以它为端点的所有边,你需要选择最少的点来覆盖所有的边二分图的最小顶点覆盖数 = 二分图的最大匹配数变种2:DAG图(无回路有向图)的最小路径覆盖路径覆盖就是在图中找一些路经,使之覆盖了图中的所有顶点,且任何一个顶点有且只有一条路径与之关联,如果把这些路径中的每条路径从它的起始点走到它的终点,那么恰好可以经过图中的每个顶点一次且仅一次DAG图的最小路径覆盖数 = 节点数(n)- 最大匹配数(m)变种3: 二分图的最大独立集:在图中选取最多的点,使任意所选两点均不相连二分图的最大独立集数 = 节点数(n)- 最大匹配数(m)*//*=***************************************************二分图匹配(匈牙利算法的DFS实现)INIT:g[][]两边定点划分的情况CALL:res=hungary();输出最大匹配数优点:适于稠密图,DFS找增广路快,实现简洁易于理解时间复杂度:O(VE);***************************************************=*/const int MAXN = 1000;int uN, vN; //u,v数目int g[MAXN][MAXN];//编号是0~n-1的int real[MAXN][MAXN];int linker[MAXN];bool used[MAXN];int n;bool dfs(int u){    int v;    for (v = 1; v <= vN; v++)        if (g[u][v] && !used[v])        {            used[v] = true;            if (linker[v] == -1 || dfs(linker[v]))            {                linker[v] = u;                return true;            }        }    return false;}int hungary(){    int res = 0;    int u;    memset(linker, -1, sizeof(linker));    for (u = 1; u <= uN; u++)    {        memset(used, 0, sizeof(used));        if (dfs(u))        {            res++;        }    }    return res;}char mat[1010][1010];int vis1[1010][1010], vis2[1010][1010];int main(){    int m;    for (int i = 0; i <= 50; i++)    {        mat[0][i] = mat[i][0] = '.';    }    while (EOF != scanf("%d %d", &n, &m))    {        for (int i = 1; i <= n; i++)        {            for (int j = 1; j <= m; j++)            {                scanf("\n%c", &mat[i][j]);            }        }        int tot1 = 1;        int tot2 = 1;        memset(vis1, 0, sizeof(vis1));        memset(vis2, 0, sizeof(vis2));        for (int i = 1; i <= n; i++)        {            for (int j = 1; j <= m; j++)            {                if (mat[i][j] == '*')                {                    if (!vis1[i][j - 1])                    {                        vis1[i][j] = tot1++;                    }                    else                    {                        vis1[i][j] = vis1[i][j - 1];                    }                    if (!vis2[i - 1][j])                    {                        vis2[i][j] = tot2++;                    }                    else                    {                        vis2[i][j] = vis2[i - 1][j];                    }                }            }        }        memset(g, 0, sizeof(g));        for (int i = 1; i <= n; i++)        {            for (int j = 1; j <= m; j++)            {                if (mat[i][j] == '*')                {                    g[vis1[i][j]][vis2[i][j]] = 1;                }            }        }        uN = max(tot1, tot2);        vN = max(tot1, tot2);        printf("%d\n", hungary());    }    return 0;} 

原创粉丝点击