UVALive 5691 二分图匹配||陈老师的多校联合20140818 J题

https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=3692

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 N x N 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$ \le$K$ \le$100) indicating the number of cases. Each case starts with an integer N ( 1$ \le$N$ \le$600) indicating the size of the square grid. Each of the following N lines contains Ncharacters 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 XM" 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
二分图裸题

#include<cstdio>
#include<iostream>
#include <string.h>
using namespace std;
const int oo=1e9;

const int mm=111111;

const int mn=999;

int node,src,dest,edge;

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

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


void prepare(int _node,int _src,int _dest)
{
    node=_node,src=_src,dest=_dest;
    for(int i=0; i<node; ++i)head[i]=-1;
    edge=0;
}
void addedge(int u,int v,int c)
{
    ver[edge]=v,flow[edge]=c,next[edge]=head[u],head[u]=edge++;
    ver[edge]=u,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++]=src]=0;
    for(l=0; l<r; ++l)
        for(i=head[u=q[l]]; i>=0; i=next[i])
            if(flow[i]&&dis[v=ver[i]]<0)
            {
                dis[q[r++]=v]=dis[u]+1;
                if(v==dest)return 1;
            }
    return 0;
}
int Dinic_dfs(int u,int exp)
{
    if(u==dest)return exp;
    for(int i=work[u],v,tmp; i>=0; i=next[i])
        if(flow[i]&&dis[v=ver[i]]==dis[u]+1&&(tmp=Dinic_dfs(v,min(exp,flow[i])))>0)
        {
            flow[i]-=tmp;
            flow[i^1]+=tmp;

            return tmp;
        }
    return 0;
}
int Dinic_flow()
{
    int i,ret=0,delta;
    while(Dinic_bfs())
    {
        for(i=0; i<node; ++i)work[i]=head[i];
        while(delta=Dinic_dfs(src,oo))ret+=delta;
    }
    return ret;
}


char a[705][705],num[705][705];
int px[4][2]= {{1,0},{0,1},{-1,0},{0,-1}};
int n;
int main()
{
    int T,tt=0;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%*c",&n);
        for(int i=0; i<n; i++)
            scanf("%s",a[i]);
        prepare(2*n*n+2,0,2*n*n+1);
        for(int i=0; i<n; i++)
            for(int j=0; j<n; j++)
            {
                if(a[i][j]=='#')
                {
                    addedge(src,i*n+j+1,1);
                    addedge(i*n+j+1+n*n,dest,1);
                    for(int k=0; k<4; k++)
                    {
                        int xx=i+px[k][0];
                        int yy=j+px[k][1];
                        if(a[xx][yy]=='#'&&xx>=0&&xx<n&&yy>=0&&yy<n)
                            addedge(i*n+j+1,xx*n+yy+1+n*n,1);
                    }
                }
            }
        printf("Case %d: %d\n",++tt,Dinic_flow()/2);
    }
    return 0;
}
/**

99
6
......
.##...
.##...
....#.
......
......

6
......
.##...
.##...
....#.
....##
......

6
......
.#....
..#...
......
.....#
......

**/


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值