HDU4185 Oil Skimming(匈牙利)

题意:

n*n的图

相邻两##可除去

问最多除去多少

奇偶建图,OK

/* ***********************************************
//Author        :devil
//Created Time  :2016/5/12 14:48:15
//************************************************ */
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <cmath>
#include <stdlib.h>
using namespace std;
const int N=605;
vector<int>eg[N*N];
char mp[N][N];
int x[N][N],y[N][N],link[N*N];
bool vis[N*N];
bool dfs(int u)
{
    for(int i=0;i<eg[u].size();i++)
    {
        int v=eg[u][i];
        if(!vis[v])
        {
            vis[v]=1;
            if(link[v]==-1||dfs(link[v]))
            {
                link[v]=u;
                return 1;
            }
        }
    }
    return 0;
}
int main()
{
    //freopen("in.txt","r",stdin);
    int t,n,cas=0;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
            scanf("%s",mp[i]+1);
        int lx=0,ly=0;
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
            {
                if(mp[i][j]==‘#‘)
                {
                    if((i+j)%2) x[i][j]=++lx;
                    else y[i][j]=++ly;
                }
            }
        }
        int m=max(lx,ly);
        memset(link,-1,sizeof(link));
        for(int i=1;i<=m;i++)
            eg[i].clear();
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
            {
                if(mp[i][j]==‘#‘)
                {
                    if(mp[i+1][j]==‘#‘)
                    {
                        if((i+j)%2) eg[x[i][j]].push_back(y[i+1][j]);
                        else eg[x[i+1][j]].push_back(y[i][j]);
                    }
                    if(mp[i][j+1]==‘#‘)
                    {
                        if((i+j)%2) eg[x[i][j]].push_back(y[i][j+1]);
                        else eg[x[i][j+1]].push_back(y[i][j]);
                    }
                }
            }
        }
        int ans=0;
        for(int i=1;i<=lx;i++)
        {
            memset(vis,0,sizeof(vis));
            ans+=dfs(i);
        }
        printf("Case %d: %d\n",++cas,ans);
    }
    return 0;
}
时间: 2024-10-05 05:50:02

HDU4185 Oil Skimming(匈牙利)的相关文章

匈牙利算法求最大匹配(HDU-4185 Oil Skimming)

如下图:要求最多可以凑成多少对对象 ? 大佬博客:https://blog.csdn.net/cillyb/article/details/55511666 模板: int link[maxn],vis[maxn]; bool dfs(int x) { for(int i = 1; i <= num; i++) { if(!vis[i] && cp[x][i]) { vis[i] = 1; if(link[i] == 0 || dfs(link[i])) { link[i] = x;

HDU4185 Oil Skimming —— 最大匹配

题目链接:https://vjudge.net/problem/HDU-4185 Oil Skimming Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3016    Accepted Submission(s): 1262 Problem Description Thanks to a certain "green" re

hdu4185 Oil Skimming(二分匹配)

# include <stdio.h> # include <algorithm> # include <string.h> using namespace std; int n,cot; int map[660],vis[660],pp[660][660],u[660][660]; int bfs(int x) { for(int i=1;i<=cot;i++) { if(!vis[i]&&pp[x][i]) { vis[i]=1; if(!ma

hdu4185 Oil Skimming

要用1×2的板子尽量多的覆盖##区域,且不能交叉,求至多可以覆盖多少板子. 每一个#向向下或向右相邻的#建边.求最大匹配就可以了. 其实这题数据是比较弱的把,应该是#的个数在600以内把.. #include <iostream> #include <cstdlib> #include <cstring> #include <string> #include <cstdio> #include <cmath> #include <

Oil Skimming HDU - 4185(匹配板题)

Oil Skimming Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3426    Accepted Submission(s): 1432 Problem Description Thanks to a certain "green" resources company, there is a new profitabl

hdu 4185 Oil Skimming(二分匹配)

Oil Skimming Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 883    Accepted Submission(s): 374 Problem Description Thanks to a certain "green" resources company, there is a new profitable

HDU 4185 ——Oil Skimming——————【最大匹配、方格的奇偶性建图】

Oil Skimming Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 4185 Description Thanks to a certain "green" resources company, there is a new profitable industry of oil skimming. There ar

J - Oil Skimming 二分图的最大匹配

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 Skimming HDU - 4185

匈牙利 #include <iostream> #include <cstdio> #include <cstring> using namespace std; char mp[660][660]; int g[660][660]; int relate[660][660]; int match[660]; int vis[660]; int k,n; int tmp; int dfs(int u){ int i; for(i = 0; i < tmp; i++