HDU 4185 Oil Skimming && 匈牙利算法

Oil Skimming

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


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

刷匈牙利算法专题中的第四题。把每个'#'编号,然后和上下左右四个方向相邻的'#'的连边,之后套一下匈牙利的模板就好了,结果就是最大匹配。数据范围看着很凶残,刚开始用邻接链表撸了一发,发现实际很水,又用邻接矩阵搞了一发

//邻接表实现的匈牙利算法
#include 
#include 
#include 
#include 
#include 
using namespace std;

const int N = 620;
char s[N][N];
int mpa[N][N];
int match[N*10];
bool use[N*10];
vector  G[N*10];

int dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0};
int cnt;

void add_edge(int a, int b)
{
    G[a].push_back(b);
    G[b].push_back(a);
}

bool dfs(int v)
{
    use[v] = true;
    for(int i = 0; i < G[v].size(); i++)
    {
        int u = G[v][i], w = match[u];
        if(w == -1 || (! use[w] && dfs(w)))
        {
            match[v] = u;
            match[u] = v;
            return true;
        }
    }
    return false;
}

int hungary()
{
    int res = 0;
    memset(match, -1, sizeof match);
    for(int i = 1; i < cnt; i++)
    {
        if(match[i] < 0)
        {
            memset(use, 0, sizeof use);
            if(dfs(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);
        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)
                            add_edge(mpa[i][j], mpa[nx][ny]);
                    }
                }
            }

        printf("Case %d: %d\n", ++x, hungary());
        for(int i = 1; i < cnt; i++)
            G[i].clear();
    }

    return 0;
}

//邻接矩阵实现的匈牙利算法
#include 
#include 
#include 
#include 
#include 
using namespace std;

const int N = 620;
char s[N][N];
int mpa[N][N];
int match[N*2];
bool use[N*2];
bool gap[N*2][N*2];
int dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0};
int cnt;

bool dfs(int v)
{
    for(int i = 1; i < cnt; i++)
    {
        if(gap[v][i] == true && use[i] == false)
        {
            use[i] = true;
            if(match[i] == -1 || dfs(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(dfs(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);
    }

    return 0;
}




你可能感兴趣的:(二分图匹配,匈牙利)