HDU 4185(匈牙利算法)

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

题目大意:给出一张图,然后问我们能够得到的有连续的两个‘#’字符组成1*2的矩形有多少个。一个字符只能用于一个矩形。

解题思路:这题是一个比较典型的二分匹配问题,在建图的时候我们将可以组成矩形的两个方块连在一起,然后就可以套匈牙利板子啦。具体看代码。
代码:

#pragma GCC optimize(2)
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <bitset>
#include <queue>
//#include <random>
#include <time.h>
using namespace std;
//#define int long long
#define ull unsigned long long
#define ls root<<1
#define rs root<<1|1
const int maxn = 5e5 + 7;
//std::mt19937 rnd(time(NULL));
char maze[710][710];
struct edge
{
    int v,next;
}e[maxn];
int match[510],head[510],cnt,used[510],vis[610][610];
void add(int a,int b)
{
    e[++cnt]=edge{b,head[a]};
    head[a]=cnt;
}
bool dfs(int u)
{
    for(int i=head[u];i;i=e[i].next){
        int v=e[i].v;
        if(used[v])continue;
        used[v]=1;
        if(!match[v] || dfs(match[v])){
            match[v]=u;
            return true;
        }
    }
    return false;
}
//O(nm) n 510  m 2040  t 100
//
int main()
{
    int t;
    scanf("%d",&t);
    for(int cases=1;cases<=t;cases++){
        int n,tot=0;
        scanf("%d",&n);
        cnt=0;
        memset(head,0,sizeof head);
        memset(match,0,sizeof match);
        memset(vis, 0, sizeof vis);
//        for (int i = 1; i <= n;i++){
//            scanf("%s", maze[i] + 1);
//        }
        for (int i = 1; i <= n; i++)
        {
            getchar();
            for (int j = 1; j <= n; j++)
            {
                scanf("%c", &maze[i][j]);
                if (maze[i][j] == '#')
                {
                    vis[i][j] = ++tot;
                    if (i > 1 && maze[i - 1][j] == '#')
                    {
                        add(vis[i - 1][j], vis[i][j]);
                        add(vis[i][j],vis[i-1][j]);
                    }
                    if (j > 1 && maze[i][j - 1] == '#')
                    {
                        add(vis[i][j - 1], vis[i][j]);
                        add(vis[i][j],vis[i][j-1]);
                    }
                }
            }
            }
        int ans=0;
        for(int i=1;i<=tot;i++){
            memset(used,0,sizeof used);
            if(dfs(i))ans++;
        }
        printf("Case %d: %d\n",cases,ans/2);
    }
}

坑边闲话:这个题我敲了一天多,我是真的没想到,一直T了一天,算了一下时间复杂度,用邻接表复杂度为O(nm),算极限的时候确实会T,然后搜题解,也没看出什么来,然后突然看到他们数组的大小开的不对,然后意识到可能是数据只有 510个左右‘#’字符,那么数组开大了,memset的操作多了也会T,哎,其实也应该想到的,因为如果按照之前算的极限数据,‘#’字符的个数最大为600*600的话,n就应该为3.6e5,那么邻接矩阵复杂度O(n3)是肯定过不了的,可能是寒假耍憨了叭。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值