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;
    }
  • 相关阅读:
    IPC机制 用Messenger进行进程间通信
    Android 远程Service
    创建前台 Service
    可见性和可达性,C#和C++
    set,map存储问题
    const形参和非const形参
    数组const形参和非const形参的区别
    switch 变量定义报错
    修改oracle用户密码永不过期
    面向对象语言成员变量方法可见性在继承中的变化
  • 原文地址:https://www.cnblogs.com/heyuhhh/p/9917220.html
Copyright © 2011-2022 走看看