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++){
        if(g[u][i] && !vis[i]){
            vis[i] = 1;
            if(!match[i] || dfs(match[i])){
                match[i] = u;
                return 1;
            }
        }
    }
    return 0;
}

int hungry(){
    int i;
    int cnt = 0;
    memset(match,0,sizeof(match));
    for(i = 0; i < tmp; i++){
        memset(vis,0,sizeof(vis));
        if(dfs(i))
            cnt++;
    }
    return cnt;
}
int main(){
    cin >> k;
    int cas = 0;
    while(k--){
        cin >> n;
        getchar();
        memset(relate,0,sizeof(relate));
        memset(g,0,sizeof(g));
        int i,j;
        tmp = 0;
        for(i = 0; i < n; i++){
            cin >> mp[i];
            getchar();
            for(j = 0; j < n; j++){
                if(mp[i][j] == '#')
                    relate[i][j] = tmp++;
            }
        }
        for(i = 0; i < n; i++){
            for(j = 0; j < n; j++){
                if(mp[i][j] != '#') continue;
                if(mp[i-1][j] == '#') g[relate[i][j]][relate[i-1][j]] = 1;
                if(mp[i+1][j] == '#') g[relate[i][j]][relate[i+1][j]] = 1;
                if(mp[i][j-1] == '#') g[relate[i][j]][relate[i][j-1]] = 1;
                if(mp[i][j+1] == '#') g[relate[i][j]][relate[i][j+1]] = 1;
            }
        }
        int ans = hungry();
        printf("Case %d: %d\n",++cas,ans/2);//除以2是因为每个点都作为起点看了一遍,即正反都看了一遍所以是真正答案的两倍
    }
    return 0;
}

HK

#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
typedef long long ll;
const int N=600*600+10;
const int M=600*600*2+10;
const int INF=0x7f7f7f7f;
const int DX[]={0,0,-1,1};
const int DY[]={-1,1,0,0};
int e[M],ne[M],h[M],idx;
void addedge(int a,int b)
{
    e[idx]=b;
    ne[idx]=h[a];
    h[a]=idx++;
}
void init()
{
    idx=0;
    memset(h,-1,sizeof(h));
}

int uN;
int Mx[N],My[N];
int dx[N],dy[N];
int dis;
bool used[N];
bool SearchP()
{
    queue<int> q;
    memset(dx,-1,sizeof(dx));
    memset(dy,-1,sizeof(dy));
    for(int i=1;i<=uN;i++)
        if(Mx[i]==-1)
        {
            q.push(i);
            dx[i]=0;
        }
    dis=INF;
    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        if(dx[u]>dis) break;
        for(int i=h[u];i!=-1;i=ne[i])
        {
            int v=e[i];
            if(dy[v]==-1)
            {
                dy[v]=dx[u]+1;
                if(My[v]==-1) dis=dy[v];
                else
                {
                    dx[My[v]]=dy[v]+1;
                    q.push(My[v]);
                }
            }
        }
    }
    return dis!=INF;
}
bool dfs(int u)
{
    for(int i=h[u];i!=-1;i=ne[i])
    {
        int v=e[i];
        if(!used[v]&&dy[v]==dx[u]+1)
        {
            used[v]=true;
            if(My[v]!=-1&&dy[v]==dis) continue;
            if(My[v]==-1||dfs(My[v]))
            {
                My[v]=u;
                Mx[u]=v;
                return true;
            }
        }
    }
    return false;
}
int MaxMatch()
{
    int res=0;
    memset(Mx,-1,sizeof(Mx));
    memset(My,-1,sizeof(My));
    while(SearchP())
    {
        memset(used,false,sizeof(used));
        for(int i=1;i<=uN;i++)
            if(Mx[i]==-1&&dfs(i)) res++;
    }
    return res;
}
char g[610][610];
int id[610][610];
int main()
{
    int n,T,kase=0;
    scanf("%d",&T);
    while(T--)
    {
        init();
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
            scanf("%s",g[i]+1);
        memset(id,-1,sizeof(id));
        int cnt=1;
        for(int i=1;i<=n;i++)
            for(int j=1;j<=n;j++)
                if(g[i][j]=='#'&&(i+j)%2==0)
                    id[i][j]=cnt++;
        uN=cnt-1;
        for(int i=1;i<=n;i++)
            for(int j=1;j<=n;j++)
                if(g[i][j]=='#'&&(i+j)%2==1)
                {
                    int t=cnt++;
                    for(int k=0;k<4;k++)
                    {
                        int ni=i+DX[k];
                        int nj=j+DY[k];
                        if(id[ni][nj]!=-1) addedge(id[ni][nj],t);
                    }
                }
        printf("Case %d: %d\n",++kase,MaxMatch());
    }
    return 0;
}

原文地址:https://www.cnblogs.com/QingyuYYYYY/p/12423139.html

时间: 2024-10-09 21:36:10

Oil Skimming HDU - 4185的相关文章

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

G - Oil Skimming - hdu 4185(二分图匹配)

题意:在大海里有一些石油 ‘#’表示石油, ‘.’表示水,有个人有一个工具可以回收这些石油,不过只能回收1*2大小的石油块,里面不能含有海水,要不就没办法使用了,求出来最多能回收多少块石油 分析:先把数据处理一下,给每一点石油都进行编号,然后查找一下四周联合是否能组成石油块,能的话就连接,因为一点有可能即在左边又在右边,所以最后的结果应该除去 2 ************************************************************************* #

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

http://acm.hdu.edu.cn/showproblem.php?pid=4185 两个挨着的'#'可以配成一对,求最多能配成几对 挨着的'#'就连边,然后求一次最大匹配,答案是最大匹配除以二(因为1 2和2 1这两对匹配实际效果是1,但是会算成2) #include <iostream> #include <cstdio> #include <cstring> using namespace std ; struct node{ int s,t,nxt ;

(匈牙利算法) hdu 4185

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

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

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 <