HDU 4185 Oil Skimming 二分图匹配

解决一个油面捞取的最大数量问题,通过构建图并使用Dinic算法进行匹配,找到最佳的油面捞取方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Oil Skimming

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


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
 

Source
 



裸的匹配。。不说了。。


#include <cstdlib>
#include <cctype>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <iostream>
#include <sstream>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <fstream>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <list>
#include <stdexcept>
#include <functional>
#include <utility>
#include <ctime>

using namespace std;

#define PB push_back
#define MP make_pair
#define CLR(vis) memset(vis,0,sizeof(vis))
#define MST(vis,pos) memset(vis,pos,sizeof(vis))
#define MAX3(a,b,c) max(a,max(b,c))
#define MAX4(a,b,c,d) max(max(a,b),max(c,d))
#define MIN3(a,b,c) min(a,min(b,c))
#define MIN4(a,b,c,d) min(min(a,b),min(c,d))
#define PI acos(-1.0)
#define INF 0x7FFFFFFF
#define LINF 1000000000000000000LL
#define eps 1e-8

typedef long long ll;
typedef unsigned long long ull;


const int mm=1111111;
const int mn=7777;

int node,s,t,edge,max_flow;

int ver[mm],cap[mm],flow[mm],next[mm];

int head[mn],work[mn],dis[mn],q[mn];

inline void init(int _node,int _s,int _t)
{
    node=_node, s=_s, t=_t;
    for(int i=0;i<node;++i)
        head[i]=-1;
    edge=max_flow=0;
}


inline void addedge(int u,int v,int c)
{
    ver[edge]=v,cap[edge]=c,flow[edge]=0,next[edge]=head[u],head[u]=edge++;
    ver[edge]=u,cap[edge]=0,flow[edge]=0,next[edge]=head[v],head[v]=edge++;
}


bool Dinic_bfs()
{
    int i,u,v,l,r=0;
    for(i=0;i<node;++i)  dis[i]=-1;
    dis[ q[r++]=s ] = 0;
    for(l=0;l<r;l++)
    {
       for(i=head[ u=q[l] ]; ~i ;i=next[i])
        if(flow[i]<cap[i] && dis[ v=ver[i] ]<0)
        {
            dis[ q[r++]=v ]=dis[u]+1;
            if(v==t) return 1;
        }
    }
    return 0;
}

int Dinic_dfs(int u,int exp)
{
    if(u==t) return exp;
    for(int &i=work[u],v,temp; ~i ;i=next[i])
    {
        if(flow[i]<cap[i] && dis[ v=ver[i] ]==dis[u]+1 && ( temp=Dinic_dfs(v,min(exp,cap[i]-flow[i])) )>0)
        {
           flow[i]+=temp;
           flow[i^1]-=temp;
           return temp;
        }
    }
    return 0;
}

int Dinic_flow()
{
    int res,i;
    while(Dinic_bfs())
    {
        for(i=0;i<node;++i) work[i]=head[i];
        while( ( res=Dinic_dfs(s,INF) ) )  max_flow+=res;
    }
    return  max_flow;
}

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

int main()
{
    int tt;
    cin>>tt;
    int n;
    char str[666][666];
    int cas=1;
    while(tt--)
    {
        cin>>n;
        int S,T;
        for(int i=1;i<=n;i++)
            scanf("%s",str[i]+1);
        S=0,T=2*n*n+1;
        init(2*n*n+2,S,T);
        //cout<<s<<" "<<t<<endl;
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
            {
                if(str[i][j]=='#')
                {
                    addedge(s,(i-1)*n+j,1);
                    addedge((i-1)*n+j+n*n,t,1);
                    for(int k=0;k<4;k++)
                    {
                        //cout<<dx[k]<<" "<<dy[k]<<endl;
                        if(i+dx[k]>=1 && i+dx[k]<=n && j+dy[k]>=1 && j+dy[k]<=n && str[i+dx[k]][j+dy[k]]=='#')
                            addedge((i-1)*n+j,(i+dx[k]-1)*n+j+dy[k]+n*n,1);
                    }
                }
            }
        }
        int ans=Dinic_flow();
        printf("Case %d: %d\n",cas++,ans/2);

    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值