zoukankan      html  css  js  c++  java
  • HDU4185(KB10-G 二分图最大匹配)

    Oil Skimming

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 2917    Accepted Submission(s): 1210


    Problem 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

    1 6 ...... .##... .##... ....#. ....## ......
     


    Sample Output

    Case 1: 3
     


    Source

     
    每个‘#’向其左边和上边的‘#’连边,然后跑最大匹配
    //2017-08-26
    #include <cstdio>
    #include <cstring>
    #include <iostream>
    #include <algorithm>
    
    using namespace std;
    
    const int N = 100000;
    const int M = 5000000;
    int head[N], tot;
    struct Edge{
        int to, next;
    }edge[M];
    
    void init(){
        tot = 0;
        memset(head, -1, sizeof(head));
    }
    
    void add_edge(int u, int v){
        edge[tot].to = v;
        edge[tot].next = head[u];
        head[u] = tot++;
    
        edge[tot].to = u;
        edge[tot].next = head[v];
        head[v] = tot++;
    }
    
    int n;
    int matching[N];
    int check[N];
    string G[700];
    int id[700][700], idcnt;
    
    bool dfs(int u){
        for(int i =  head[u]; i != -1; i = edge[i].next){
            int v = edge[i].to;
            if(!check[v]){//要求不在交替路
                check[v] = 1;//放入交替路
                if(matching[v] == -1 || dfs(matching[v])){
                    //如果是未匹配点,说明交替路为增广路,则交换路径,并返回成功
                    matching[u] = v;
                    matching[v] = u;
                    return true;
                }
            }
        }
        return false;//不存在增广路
    }
    
    //hungarian: 二分图最大匹配匈牙利算法
    //input: null
    //output: ans 最大匹配数
    int hungarian(){
        int ans = 0;
        memset(matching, -1, sizeof(matching));
        for(int u = 1; u < idcnt; u++){
            if(matching[u] == -1){
                memset(check, 0, sizeof(check));
                if(dfs(u))
                  ans++;
            }
        }
        return ans;
    }
    
    int main()
    {
        std::ios::sync_with_stdio(false);
        //freopen("inputG.txt", "r", stdin);
        int T, kase = 0;
        cin>>T;
        while(T--){
            cin>>n;
            init();
            idcnt = 1;
            for(int i = 0; i < n; i++){
                  cin>>G[i];
                for(int j = 0; j < n; j++){
                    if(G[i][j] == '#'){
                        id[i][j] = idcnt++;
                        if(i-1>=0 && G[i-1][j] == '#')
                              add_edge(id[i][j], id[i-1][j]);
                        if(j-1>=0 && G[i][j-1] == '#')
                              add_edge(id[i][j], id[i][j-1]);
                    }
                }
            }
            cout<<"Case "<<++kase<<": "<<hungarian()<<endl;
        }
    
        return 0;
    }
  • 相关阅读:
    Java线上应用故障排查之一:高CPU占用【转】
    JAVA 之 Tomcat知识框架【转】
    FileZilla 配置备份与还原【转】
    linux删除第几天日志【原创】
    sar命令使用【转】
    Mysql Group Replication 简介及单主模式组复制配置【转】
    grep 中的正则表达式【转】
    java程序out of memory【转】
    Java笔记18:JUnit单元测试
    Java笔记17:导出可执行jar包
  • 原文地址:https://www.cnblogs.com/Penn000/p/7434873.html
Copyright © 2011-2022 走看看