HDU 4185 Oil Skimming(离散化 + 二分图匹配)

#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>
#include <algorithm>
#define LL long long
#define FOR(i, x, y) for(int i=x;i<=y;i++)
using namespace std;
const int MAXN = 600 + 10;
int G[MAXN][MAXN];
int vis[MAXN];
int match[MAXN];
int id[MAXN][MAXN];
int n, m;
char s[MAXN][MAXN];
int path(int u)
{
    for(int v=1;v<=m;v++)
    {
        if(G[u][v] && !vis[v])
        {
            vis[v] = 1;
            if(match[v] == -1 || path(match[v]))
            {
                match[v] = u;
                return 1;
            }
        }
    }
    return 0;
}
int main()
{
    int T, kcase = 1;
    scanf("%d", &T);
    while(T--)
    {
        int N;
        scanf("%d", &N);
        memset(id, 0, sizeof(id));
        int x = 0;
        for(int i=1;i<=N;i++)
        {
            scanf("%s", s[i] + 1);
            for(int j=1;j<=N;j++)
            {
                if(s[i][j] == '#')
                    id[i][j] = ++x;
            }
        }
        n = m = x;
        memset(G, 0, sizeof(G));
        FOR(i, 1, N)
        {
            FOR(j, 1, N)
            {
                if(s[i][j] == '#')
                {
                    if(i > 1 && s[i-1][j] == '#')
                        G[id[i-1][j]][id[i][j]] = 1;
                    if(i < N && s[i+1][j] == '#')
                        G[id[i+1][j]][id[i][j]] = 1;
                    if(j > 1 && s[i][j-1] == '#')
                        G[id[i][j-1]][id[i][j]] = 1;
                    if(j < N && s[i][j+1] == '#')
                        G[id[i][j+1]][id[i][j]] = 1;
                }
            }
        }
        int ans = 0;
        memset(match, -1, sizeof(match));
        for(int i=1;i<=n;i++)
        {
            memset(vis, 0, sizeof(vis));
            ans += path(i);
        }
        printf("Case %d: %d\n", kcase++, ans / 2);
    }
    return 0;
}

时间: 2024-10-14 18:13:24

HDU 4185 Oil Skimming(离散化 + 二分图匹配)的相关文章

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

HDU 4185 Oil Skimming

二分图的最大匹配 #include<stdio.h> #include<iostream> #include<algorithm> #include<string.h> #include<vector> using namespace std; const int MAXN=1505; int linker[MAXN]; bool used[MAXN]; vector<int>map[MAXN]; int uN; char m[MAX

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

hdu 3829 Cat VS Dog 二分图匹配 最大点独立集

Cat VS Dog Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 125536/65536 K (Java/Others) Problem Description The zoo have N cats and M dogs, today there are P children visiting the zoo, each child has a like-animal and a dislike-animal, if the

HDU 5093 Battle ships(二分图匹配)

该题是一道经典的二分图匹配题目 .  同一列(行)上不能放两个船除非有冰山隔着.对于这种二维平面图,我们很容易想到将行和列分成两个集合,进行二分图匹配,当一个行坐标匹配到一个列坐标时,该格子可以放置船.那么为了使任意两个船都不在同一行或者同一列,除非有冰山,我们可以将每一行中一块连续的只能放置一个船的区域都设成一个编号,同样的按照列也这样处理,这样就相当于将行和列缩点了,接下来用最大流模板套一套就可以了 . 处理二分图还有一种更好的算法,叫匈牙利算法,紫书上没有,先用最大流算法解决吧 . 紫书十

hdu 5727 Necklace 阴阳珠 二分图匹配+暴力全排列

Necklace Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 2462    Accepted Submission(s): 775 Problem Description SJX has 2*N magic gems. N of them have Yin energy inside while others have Yang e

HDU 2236 无题II(二分图匹配+二分)

HDU 2236 无题II 题目链接 思路:行列只能一个,想到二分图,然后二分区间长度,枚举下限,就能求出哪些边是能用的,然后建图跑二分图,如果最大匹配等于n就是符合的 代码: #include <cstdio> #include <cstring> #include <vector> #include <algorithm> using namespace std; const int N = 105; int t, n, x[N][N], have[N]

HDU 1150 Machine Schedule(二分图匹配)

解题思路: 本题要求的为最小点覆盖,最小点覆盖 == 最大匹配,要注意初始为模式0,没有消耗,所以模式0不需要连边. #include <iostream> #include <cstring> #include <cstdlib> #include <cstdio> #include <algorithm> #include <vector> #include <cmath> #include <queue>