zoukankan      html  css  js  c++  java
  • HDU 4185 ——Oil Skimming——————【最大匹配、方格的奇偶性建图】

    Oil Skimming
    Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

    Description

    Thanks to a certain "green" resources company, there is a new profitable industry of oil skimming. There are large slicks of crude oil floating in the Gulf of Mexico just waiting to be scooped up by enterprising oil barons. One such oil baron has a special plane that can skim the surface of the water collecting oil on the water's surface. However, each scoop covers a 10m by 20m rectangle (going either east/west or north/south). It also requires that the rectangle be completely covered in oil, otherwise the product is contaminated by pure ocean water and thus unprofitable! Given a map of an oil slick, the oil baron would like you to compute the maximum number of scoops that may be extracted. The map is an NxN grid where each cell represents a 10m square of water, and each cell is marked as either being covered in oil or pure water.
     

    Input

    The input starts with an integer K (1 <= K <= 100) indicating the number of cases. Each case starts with an integer N (1 <= N <= 600) indicating the size of the square grid. Each of the following N lines contains N characters that represent the cells of a row in the grid. A character of '#' represents an oily cell, and a character of '.' represents a pure water cell.
     

    Output

    For each case, one line should be produced, formatted exactly as follows: "Case X: M" where X is the case number (starting from 1) and M is the maximum number of scoops of oil that may be extracted.
     

    Sample Input

    6
    ......
    .##...
    .##...
    ....#.
    ....##
    ......
     

    Sample Output

    Case 1: 3
     
     
    题目大意:抽象模型。用1*2的长条覆盖图中的"#",覆盖后变为"."不能覆盖到“.”。问你最多需要多少次覆盖。
     
    解题思路:最小顶点覆盖:用最少的点,让每条边至少一个端点被覆盖(选中)。我们可以按照格子的奇偶性划分二部。如果相邻位置都有“#”,就连一条边。最后求最大匹配即可。
     
     
    #include<stdio.h>
    #include<string.h>
    #include<math.h>
    #include<queue>
    #include<vector>
    #include<algorithm>
    using namespace std;
    const int maxn = 660;
    const int INF = 0x3f3f3f3f;
    vector<int>G[maxn];
    int Mx[maxn], My[maxn], dx[maxn], dy[maxn], used[maxn], dis;
    char Map[maxn][maxn];
    int lis[maxn][maxn];
    bool SearchP(int _n){
        queue<int>Q;
        memset(dx,-1,sizeof(dx));
        memset(dy,-1,sizeof(dy));
        int dis = INF;
        for(int i = 1; i <= _n; i++){
            if(Mx[i] == -1){
                dx[i] = 0;
                Q.push(i);
            }
        }
        int v;
        while(!Q.empty()){
            int u = Q.front(); Q.pop();
            if(dx[u] > dis) break;
            for(int i = 0; i < G[u].size(); i++){
                v = G[u][i];
                if(dy[v] == -1){
                    dy[v] = dx[u] + 1;
                    if(My[v] == -1){
                        dis = dy[v];
                    }else{
                        dx[My[v]] = dy[v] + 1;
                        Q.push(My[v]);
                    }
                }
            }
        }
        return dis != INF;
    }
    int dfs(int u){
        int v;
        for(int i = 0; i < G[u].size(); i++){
            v = G[u][i];
            if(!used[v] && dy[v] == dx[u] + 1){
                used[v] = 1;
                if(My[v] != -1 && dy[v] == dis){
                    continue;
                }
                if(My[v] == -1 || dfs(My[v])){
                    Mx[u] = v;
                    My[v] = u;
                    return true;
                }
            }
        }
        return false;
    }
    int MaxMatch(int ln,int rn){
        int ret = 0;
        memset(Mx,-1,sizeof(Mx));
        memset(My,-1,sizeof(My));
        while(SearchP(ln)){
            memset(used,0,sizeof(used));
            for(int i = 1; i <= ln; i++){
                if(Mx[i] == -1 && dfs(i)){
                    ret++;
                }
            }
        }
        return ret;
    }
    int main(){
        int T, cas = 0, n, m, N;
        scanf("%d",&T);
        while(T--){
            n =  m = 0;
            scanf("%d",&N);
            int N2 = N*N;
            for(int i = 0; i <= N2; i++){
                G[i].clear();
            }
            for(int i = 0; i <= N+1; i++){
                Map[i][0] = '.';
                Map[0][i] = '.';
                Map[N+1][i] = '.';
                Map[i][N+1] = '.';
            }
            for(int i = 1; i<= N; i++){
                getchar();
                for(int j = 1; j <= N; j++){
                    scanf("%c",&Map[i][j]);
                    if(Map[i][j] == '#'){
                        if((i+j)%2 == 0){
                            ++n;
                            lis[i][j] = n;
                            if(Map[i-1][j] == '#'){
                                G[n].push_back(lis[i-1][j]);
                            }
                            if(Map[i][j-1] == '#'){
                                G[n].push_back(lis[i][j-1]);
                            }
                        }else{
                            ++m;
                            lis[i][j] = m;
                            if(Map[i-1][j] == '#'){
                                G[lis[i-1][j]].push_back(m);
                            }
                            if(Map[i][j-1] == '#'){
                                G[lis[i][j-1]].push_back(m);
                            }
                        }
                    }
                }
            }
            int res = MaxMatch(n,m);
            printf("Case %d: %d
    ",++cas,res);
        }
        return 0;
    }
    /*
    4
    .##.
    ..#.
    .###
    ....
    4
    .##.
    ..#.
    .###
    ..#.
    
    4
    .##.
    ..#.
    .###
    ...#
    */
    

      

     
  • 相关阅读:
    教你50招提升ASP.NET性能(二十一):避免使用会话状态
    教你50招提升ASP.NET性能(二十):7条便利的ViewState技巧
    教你50招提升ASP.NET性能(二十):认识你的循环
    教你50招提升ASP.NET性能(十九):静态集合
    教你50招提升ASP.NET性能(十八):在处理网站性能问题前,首先验证问题是否出在客户端
    教你50招提升ASP.NET性能(十七):不要认为问题只会从业务层产生
    教你50招提升ASP.NET性能(十六):把问题仍给硬件而不是开发人员
    教你50招提升ASP.NET性能(十五):解决性能问题时不要低估UI的价值
    教你50招提升ASP.NET性能(十四):使用startMode属性来减少ASP.NET站点加载时间
    Chrome谷歌浏览器书签排序后,重启浏览器导致排序无效的问题(完美解决)
  • 原文地址:https://www.cnblogs.com/chengsheng/p/4955467.html
Copyright © 2011-2022 走看看