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;
    }
  • 相关阅读:
    valgrind内存检测
    uosdeepin商店下载的软件deb包位置
    jmeter中获取到token,又因为现在都是JWT的所以需要添加Bearer 和获取到的token进行拼接
    jmeter json提取器
    jmeter命令行启动
    linux 中把文件内容变成空,不删除文件
    94--分布式事务五-Seata AT模式-Spring Cloud微服务案例(添加AT事务)
    94--Docker(概念/镜像操作/数据管理/网络/互联/构建镜像)
    93--分布式事务四-Seata AT模式-Spring Cloud微服务案例(无事务)
    93--分布式事务二-Seata AT 模式
  • 原文地址:https://www.cnblogs.com/heyuhhh/p/9917220.html
Copyright © 2011-2022 走看看