zoukankan      html  css  js  c++  java
  • HDU4185:Oil Skimming(二分图最大匹配)

    Oil Skimming

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

    题目链接:acm.hdu.edu.cn/showproblem.php?pid=4185

    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
     
    题解:
    每个‘#’看出一个点,然后对挨着的‘#’进行二分图的最大匹配,双向图最后结果除以2
     
    代码如下
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <iostream>
    #define mem(x) memset(x,0,sizeof(x))
    using namespace std;
    
    const int N = 605 ;
    int check[N],match[N],map[N][N],link[N][N];
    int Case,n,tot,ans,dfn,t=0;
    
    inline void update(int x,int y){
        if(map[x+1][y]) link[map[x][y]][map[x+1][y]]=1;
        if(map[x-1][y]) link[map[x][y]][map[x-1][y]]=1;
        if(map[x][y+1]) link[map[x][y]][map[x][y+1]]=1;
        if(map[x][y-1]) link[map[x][y]][map[x][y-1]]=1;
    }
    
    inline int dfs(int x){
        for(int i=1;i<=tot;i++){
            if(link[x][i] && check[i]!=dfn){
                check[i]=dfn;
                if(match[i]==0 || dfs(match[i])){
                    match[i]=x;
                    return 1;
                }
            }
        }
        return 0;
    }
    int main(){
        scanf("%d",&Case);
        while(Case--){
            t++;
            scanf("%d",&n);
            mem(map);mem(match);mem(link);tot=0;ans=0;dfn=0;mem(check);
            for(int i=1;i<=n;i++){
                char s[N];
                scanf("%s",s+1);
                for(int j=1;j<=n;j++){
                    if(s[j]=='#') map[i][j]=++tot;
                }
            }
            for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) if(map[i][j]) update(i,j);
            for(int i=1;i<=tot;i++){
                dfn++;
                if(dfs(i)) ans++;
            }
            printf("Case %d: %d
    ",t,ans/2);
        }
        return 0;
    }
  • 相关阅读:
    Python+request+unittest实现接口测试框架集成实例
    真正解决Jenkins安装插件总是报错的问题(网上查的解决方案都无效)
    Rancher 2.2.2
    Rancher管理k8s集群
    清理cosbench工具所占用磁盘空间的小脚本
    一个检查本机和远程机器的根目录所在磁盘的剩余空间的bash小脚本
    当df命令hang住了, 怎么办?
    OpenCV操作像素的几种方法(单个像素|操作多像素|遍历像素)
    OpenCV-颜色通道的分离、合并(转)
    ImageMagick 提取四通道png图片的alpha及magick使用
  • 原文地址:https://www.cnblogs.com/heyuhhh/p/9917220.html
Copyright © 2011-2022 走看看