zoukankan      html  css  js  c++  java
  • TTTTTTTTTTTTTTTTT POJ 2226 草地覆木板 二分匹配 建图

    Muddy Fields
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 9754   Accepted: 3618

    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

    题意:一个row*col的矩阵表示一块田地,'*'表示湿地,'.'表示草地。现在FJ要在田地上铺木板盖掉所有的湿地,露出所有的草地。每块木板的宽度为1,长度为任意长。问FJ最少用几块木板就可以完成任务?
    #include <cstdio>
    #include <algorithm>
    #include <cstring>
    #include <iostream>
    #include <cmath>
    #include <vector>
    using namespace std;
    
    vector<int> G[5005];
    int match[5005],used[5005];
    int n,r,c,cntl,cntr,m;
    int nel[55][55],ner[55][55];
    
    void add_edge(int u,int v)
    {
        G[u].push_back(v);
        G[v].push_back(u);
    }
    
    bool dfs(int u)
    {
         used[u]=1;
         for(int i=0;i<G[u].size();i++)
         {
           int v=G[u][i];
           int w=match[v];
           if(w<0||!used[w]&&dfs(w))
             {
                 match[u]=v;
                 match[v]=u;
                 return true;
             }
         }
         return false;
    }
    
    int  bipartite_match()
    {
        memset(match,-1,sizeof(match));
        int res=0;
        for(int i=1;i<=cntl+cntr;i++)
          if(match[i]<0)
          {
              memset(used,0,sizeof(used));
              if(dfs(i)) res++;
          }
        return res;
    }
    
    char s[55][55];
    
    void build()
    {
        for(int i=1;i<=cntl+cntr;i++) G[i].clear();
        for(int i=1;i<=n;i++)
            for(int j=1;j<=m;j++)
             if(s[i][j]=='*')
                 add_edge(nel[i][j],cntl+ner[i][j]);
    }
    
    int main()
    {
        while(~scanf("%d %d",&n,&m))
        {
            cntl=0;cntr=0;
            for(int i=1;i<=n;i++)
                {
                    scanf("%s",s[i]+1);
                    for(int j=1;j<=m;)
                       {
                         while(s[i][j]=='.'&&j<=m)  j++;
                         if(j>m) break;
                         cntl++;
                         while(s[i][j]=='*')
                            {
                                nel[i][j]=cntl;
                                j++;
                            }
                       }
                }
            for(int j=1;j<=m;j++)
                {
                    for(int i=1;i<=n;)
                       {
                         while(s[i][j]=='.'&&i<=n)  i++;
                         if(i>n) break;
                         cntr++;
                         while(s[i][j]=='*')
                            {
                                ner[i][j]=cntr;
                                i++;
                            }
                       }
                }
             build();
             printf("%d
    ",bipartite_match());
        }
        return 0;
    }
    

      分析:爽啊,这道题算是自己没看题解独立完成的吧,水平开始有点出来了

    这道题跟以前做的那道毁灭星球的题目最大的不同就是要让矩阵中的草地都露出来,这就决定了他的

    建图方式有些不太一样,建图:首先预处理出每个顶点在行中属于的标号,与在列中属于的标号,标号时对于连续的一段进行合并,这一段连续的*标号成同一个数字(这也是能进行二分匹配的原因,因为能用同一块木板),接下来建图,左边代表行标号,右边代表列标号,根据二分图中最小顶点覆盖等于最大匹配数即可。

    然后

  • 相关阅读:
    同一域环境下SQLServer DB Failover故障转移配置详解
    WebAPI项目中使用SwaggerUI
    Failed to initialize the Common Language Runtime
    WCF Throttling 限流的三道闸口
    Entity Framework 乐观并发控制
    MVC3不能正确识别JSON中的Enum枚举值
    编写高质量代码改善C#程序的157个建议读书笔记【11-20】
    如果下次做模板,我就使用Nvelocity
    对于react中的this.setState的理解
    对于react中rredux的理解
  • 原文地址:https://www.cnblogs.com/smilesundream/p/5505189.html
Copyright © 2011-2022 走看看