1. 程式人生 > >G - Oil Skimming ——二分圖匹配(匈牙利演算法)

G - Oil Skimming ——二分圖匹配(匈牙利演算法)

G - 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

思路:

       是把 ‘#’  替換成 數字,構建 關係G(本程式碼是 line[][]) 。

注意:

        1)二分匹配時,要記得把n換成tmp。  詳細見程式碼

        2)記得清零;

總結:

       一個二分圖中的最大匹配數等於這個圖中的 最小點覆蓋數——假如選了一個點就相當於覆蓋了以它為端點的所有邊,你需要選擇最少的點來覆蓋所有的邊。

程式碼:

#include<map>
#include<queue>
#include<math.h>
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
#define MM 605
const int INF=1e9;
int line[MM][MM],book[MM],man[MM],t[MM][MM];
char tu[MM][MM];
int n;
bool Find(int x)
{
    for(int i=1; i<=n; i++)
    {
        if(line[x][i] && !book[i])
        {
            book[i]=1;
            if(!man[i] || Find(man[i]))
            {
                man[i]=x;
                return true;
            }
        }
    }
    return false;
}
int match()
{
    int ans=0;
    for(int i=1; i<=n; i++)
    {
        mem(book,0);
        if(Find(i))
            ans++;
    }
    return ans/2;
}
int main()
{
    int ca,cas=1;
    scanf("%d",&ca);
    while(ca--)
    {
        mem(tu,'.');
        mem(t,0);
        mem(line,0);
        mem(man,0);
        int tmp=0;
        scanf("%d",&n);
        for(int i=1; i<=n; i++)
        {
            scanf("%s",tu[i]+1);
            for(int j=1; j<=n; j++)
            {
                if(tu[i][j]=='#')
                    t[i][j]=++tmp;
            }
        }
        for(int i=1; i<=n; i++)
        {
            for(int j=1; j<=n; j++)
            {
                if(tu[i][j]=='#')
                {
                    if(tu[i-1][j]=='#')
                        line[t[i][j]][t[i-1][j]]=1;
                    if(tu[i][j-1]=='#')
                        line[t[i][j]][t[i][j-1]]=1;
                    if(tu[i+1][j]=='#')
                        line[t[i][j]][t[i+1][j]]=1;
                    if(tu[i][j+1]=='#')
                        line[t[i][j]][t[i][j+1]]=1;
                }
            }
        }
        n=tmp;
        printf("Case %d: %d\n",cas++,match());
    }
    return 0;
}