HDU - 4185 - Oil Skimming(二分图最大分配)

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
题目链接
参考题解1
参考题解2
(这里找了两个题解,我看的那个参考的题解实在是找不到了,实在有点对不起原作者)
匈牙利算法(求最大分配)

题目给你一个地图,“.”代表是水,“#”代表是油,每一个字符代表1010m的方形水池,问用1020m的器物最多能收集几次油,每一次收集量只能是10*20。
最大分配问题,对于每个"#"都标个号,然后记录一下相邻的,连个线,看最多连多少。
AC代码:

//邻接矩阵实现的匈牙利算法
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int maxn = 6e2 + 5;
char s[maxn][maxn];
int mpa[maxn][maxn];
int match[maxn << 1];
bool use[maxn << 1];
bool gap[maxn << 1][maxn << 1];
int dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0};
int cnt;

bool route(int v)
{
    for(int i = 1; i < cnt; i++)
    {
        if(gap[v][i] == true && use[i] == false)	//两者有连线并且这个点还没有被用过
        {
            use[i] = true;
            if(match[i] == -1 || route(match[i]))	//匹配对象还没有占有者,或者能被腾出来
            {
                match[i] = v;
                return true;
            }
        }
    }

    return false;
}

//匈牙利算法
int hungary()
{
    int res = 0;
    memset(match, -1, sizeof match);
    for(int i = 1; i < cnt; i++)
    {
        memset(use, 0, sizeof(use));
        if(route(i)) res++;
    }

    return res;
}

int main()
{
    int t, n, x = 0;
    scanf("%d", &t);
    while(t--)
    {
        scanf("%d", &n);
        for(int i = 0; i < n; i++)
            scanf(" %s", s[i]);

        memset(mpa, 0, sizeof mpa);
        memset(gap, 0, sizeof gap);
        cnt = 1;
        for(int i = 0; i < n; i++)
            for(int j = 0; j < n; j++)
                if(s[i][j] == '#')
                    mpa[i][j] = cnt++;

        for(int i = 0; i < n; i++)
            for(int j = 0; j < n; j++)
            {
                if(mpa[i][j] != 0)
                {
                    for(int k = 0; k < 4; k++)	//遍历四个方向,这样下来其实就是为每一两个相邻边之间连了双向边
                    {
                        int nx = i + dx[k], ny = j + dy[k];
                        if(nx >= 0 && nx < n && ny >= 0 && ny < n && mpa[nx][ny] != 0)
                            gap[mpa[i][j]][mpa[nx][ny]] = true;
                    }
                }
            }

        printf("Case %d: %d\n", ++x, hungary() / 2);	//每一次连边的时候都是连的双向变,所以除以2
    }

    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值