zoukankan      html  css  js  c++  java
  • poj 2226 Muddy Fields(水二分图)

    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.
     
    虽然想到了做法 但没想到和这题的联系 细想才明白过来
    一个提示
     
    只要涉及这种类型的题 一套Dinic就完事了、
     
    每一行连续的点缩为一个点 放在左边
    每一列连续的点缩为一个点 放在右边
    匹配就好了
    这里用的Dinic  匈牙利和hc也行
    #include <iostream>
    #include <cstdio>
    #include <sstream>
    #include <cstring>
    #include <map>
    #include <cctype>
    #include <set>
    #include <vector>
    #include <stack>
    #include <queue>
    #include <algorithm>
    #include <cmath>
    #include <bitset>
    #define rap(i, a, n) for(int i=a; i<=n; i++)
    #define rep(i, a, n) for(int i=a; i<n; i++)
    #define lap(i, a, n) for(int i=n; i>=a; i--)
    #define lep(i, a, n) for(int i=n; i>a; i--)
    #define rd(a) scanf("%d", &a)
    #define rlld(a) scanf("%lld", &a)
    #define rc(a) scanf("%c", &a)
    #define rs(a) scanf("%s", a)
    #define rb(a) scanf("%lf", &a)
    #define rf(a) scanf("%f", &a)
    #define pd(a) printf("%d
    ", a)
    #define plld(a) printf("%lld
    ", a)
    #define pc(a) printf("%c
    ", a)
    #define ps(a) printf("%s
    ", a)
    #define MOD 2018
    #define LL long long
    #define ULL unsigned long long
    #define Pair pair<int, int>
    #define mem(a, b) memset(a, b, sizeof(a))
    #define _  ios_base::sync_with_stdio(0),cin.tie(0)
    //freopen("1.txt", "r", stdin);
    using namespace std;
    const int maxn = 10000, INF = 0x7fffffff;
    int n, m;
    
    char str[55][55];
    int nu1[55][55], nu2[55][55];
    
    int vis[maxn], d[maxn], nex[maxn], head[maxn], cnt, cur[maxn];
    int s, t;
    
    struct node
    {
        int u, v, c;
    }Node[maxn << 1];
    
    void add_(int u, int v, int c)
    {
        Node[cnt].u = u;
        Node[cnt].v = v;
        Node[cnt].c = c;
        nex[cnt] = head[u];
        head[u] = cnt++;
    }
    
    void add(int u, int v, int c)
    {
        add_(u, v, c);
        add_(v, u, 0);
    }
    
    bool bfs()
    {
        mem(d, 0);
        queue<int> Q;
        Q.push(s);
        d[s] = 1;
        while(!Q.empty())
        {
            int u = Q.front(); Q.pop();
            for(int i = head[u]; i != -1; i = nex[i])
            {
                int v = Node[i].v;
                if(!d[v] && Node[i].c > 0)
                {
                    d[v] = d[u] + 1;
                    Q.push(v);
                    if(v == t) return 1;
                }
            }
        }
        return d[t] != 0;
    }
    
    int dfs(int u, int cap)
    {
        int ret = 0;
        if(u == t || cap == 0)
            return cap;
        for(int &i = cur[u]; i != -1; i = nex[i])
        {
            int v = Node[i].v;
            if(d[v] == d[u] + 1 && Node[i].c > 0)
            {
                int V = dfs(v, min(cap, Node[i].c));
                Node[i].c -= V;
                Node[i ^ 1].c += V;
                cap -= V;
                ret += V;
                if(cap == 0) break;
            }
        }
        return ret;
    }
    
    int Dinic()
    {
        int ret = 0;
        while(bfs())
        {
            memcpy(cur, head, sizeof(head));
            ret += dfs(s, INF);
        }
        return ret;
    }
    
    
    int main()
    {
        cnt = 0;
        mem(head, -1);
        rd(n), rd(m);
        s = 0, t = n * m + 1;
        rep(i, 0, n)
            scanf("%s", str[i]);
        int ans = 0;
        bool flag = 0;
        rep(i, 0, n)
        {
            //int j = 0;
            rep(j, 0, m)
            {
                if(str[i][j] == '*')
                {
                    if(j == 0 || str[i][j] != str[i][j - 1])
                        ans++;
    
                    nu1[i][j] = ans;
                }
            }
        }
    
        rap(i, 1, ans)
            add(s, i, 1);
        int ss = ans + 1;
        rep(j, 0, m)
        {
            rep(i, 0, n)
            {
                if(str[i][j] == '*')
                {
    
                    if(i == 0 || str[i][j] != str[i - 1][j])
                        ans++;
                    nu2[i][j] = ans;
                }
            }
        }
        rap(i, ss, ans)
            add(i, t, 1);
        rep(i, 0, n)
        {
    
            rep(j, 0, m)
            {
                if(str[i][j] == '*')
                    add(nu1[i][j], nu2[i][j], 1);
            }
        }
        pd(Dinic());
    
    
        return 0;
    }
     
     
    自己选择的路,跪着也要走完。朋友们,虽然这个世界日益浮躁起来,只要能够为了当时纯粹的梦想和感动坚持努力下去,不管其它人怎么样,我们也能够保持自己的本色走下去。
  • 相关阅读:
    RedissonConfProperty
    IdGenerator(雪花)
    Btrace和arthas地址
    SqlFilter
    AuthorityFilter
    111
    分布式数据库-杂记
    站点集群
    分布式精华文章
    高并发
  • 原文地址:https://www.cnblogs.com/WTSRUVF/p/10694039.html
Copyright © 2011-2022 走看看