Battle ships(网络流)

Battle ships

Time Limit: 2000/1000 MS (Java/Others)
Memory Limit: 65536/65536 K (Java/Others)

Problem Description

Dear contestant, now you are an excellent navy commander, who is responsible of a tough mission currently.

Your fleet unfortunately encountered an enemy fleet near the South Pole where the geographical conditions are negative for both sides. The floating ice and iceberg blocks battleships move which leads to this unexpected engagement highly dangerous, unpredictable and incontrollable.

But, fortunately, as an experienced navy commander, you are able to take opportunity to embattle the ships to maximize the utility of cannons on the battleships before the engagement.

The target is, arrange as many battleships as you can in the map. However, there are three rules so that you cannot do that arbitrary:

A battleship cannot lay on floating ice
A battleship cannot be placed on an iceberg

Two battleships cannot be arranged in the same row or column, unless one or more icebergs are in the middle of them.

Input

There is only one integer T (0

Output

For each case, output just one line, contains a single integer which represents the maximal possible number of battleships can be arranged.

Sample Input
2
4 4
*ooo
o###
**#*
ooo*
4 4
#***
*#**
**#*
ooo#
Sample Output
3
5

题目大意:

在地图上摆船,只有*上才能摆,每行每列最多只有一艘船,除非中间隔了个#。









解:

二分图的一般模型。把图拆成行列,然后缩点,行连源点,列连汇点。一个*会在某一行某一列上,然后我们把这行这列连起来。跑最大流即可。
为什么这样是对的?
我们增广一次,有一行一列满流,表示这一行这一列上有船了。最大流表示放的船数。
code:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
struct lxy{
    int to,flow,next;
}eg[1000005];

int T,n,m,head[2005],ss,tt,ct,mid,cnt;
char s[55][55];
int hang[55][55],lie[55][55];
int layer[2005];

void add(int op,int ed,int len){
    eg[++cnt].next=head[op];
    eg[cnt].to=ed;
    eg[cnt].flow=len;
    head[op]=cnt;
}

bool bfs(){
    memset(layer,0,sizeof(layer));
    queue <int> d;
    d.push(ss);layer[ss]=1;
    while(!d.empty()){
        int now=d.front();d.pop();
        for(int i=head[now];i!=-1;i=eg[i].next){
            if(eg[i].flow!=0&&layer[eg[i].to]==0){
                d.push(eg[i].to);
                layer[eg[i].to]=layer[now]+1;
            }
        }
    }
    return layer[tt];
}

int dfs(int u,int a)
{
    if(u==tt||a==0) return a;
    int f,flow=0;
    for(int i=head[u];i!=-1;i=eg[i].next){
        if(layer[eg[i].to]==layer[u]+1&&eg[i].flow!=0){
            f=dfs(eg[i].to,min(eg[i].flow,a));
            flow+=f;a-=f;
            eg[i].flow-=f;eg[i^1].flow+=f;
            if(a==0) break;
        }
    }
    return flow;
}

int dinic()
{
    int ret=0;
    while(bfs()){
        ret+=dfs(ss,0x3f3f3f3f);
    }
    return ret;
}

int main()
{
    scanf("%d",&T);
    while(T--){
    memset(head,-1,sizeof(head));
    memset(eg,0,sizeof(eg));
    memset(hang,0,sizeof(hang));
    memset(lie,0,sizeof(lie));
    ct=0,cnt=-1,mid=0,ss=0,tt=0;
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
      scanf("%s",s[i]+1);
    for(int i=1;i<=n;i++)
      for(int j=1;j<=m;j++){
        if(j==1||(s[i][j]=='#'&&s[i][j-1]!='#')) ct++;
        if(s[i][j]=='*') hang[i][j]=ct;
      }
    ss=0;mid=ct;
    for(int i=1;i<=ct;i++) add(ss,i,1),add(i,ss,0);
    for(int i=1;i<=m;i++)
      for(int j=1;j<=n;j++){
        if(j==1||(s[j][i]=='#'&&s[j-1][i]!='#')) ct++;
        if(s[j][i]=='*') lie[j][i]=ct;
      }
    tt=ct+1;
    for(int i=mid+1;i<=ct;i++) add(i,tt,1),add(tt,i,0);
    for(int i=1;i<=n;i++)
      for(int j=1;j<=m;j++)
        if(s[i][j]=='*'){
            add(hang[i][j],lie[i][j],1);
            add(lie[i][j],hang[i][j],0);
        }
    printf("%d\n",dinic());
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值