hdu 3046

来源:互联网 发布:电信运营商网络架构 编辑:程序博客网 时间:2024/05/21 12:48

Pleasant sheep and big big wolf

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


Problem Description
In ZJNU, there is a well-known prairie. And it attracts pleasant sheep and his companions to have a holiday. Big big wolf and his families know about this, and quietly hid in the big lawn. As ZJNU ACM/ICPC team, we have an obligation to protect pleasant sheep and his companions to free from being disturbed by big big wolf. We decided to build a number of unit fence whose length is 1. Any wolf and sheep can not cross the fence. Of course, one grid can only contain an animal.
Now, we ask to place the minimum fences to let pleasant sheep and his Companions to free from being disturbed by big big wolf and his companions. 


 

Input
There are many cases. 
For every case: 

N and M(N,M<=200)
then N*M matrix: 
0 is empty, and 1 is pleasant sheep and his companions, 2 is big big wolf and his companions.
 

Output
For every case:

First line output “Case p:”, p is the p-th case; 
The second line is the answer. 
 

Sample Input
4 61 0 0 1 0 00 1 1 0 0 02 0 0 0 0 00 2 0 1 1 0
 

Sample Output
Case 1:4
 

Source
2009 Multi-University Training Contest 14 - Host by ZJNU
 

Recommend
gaojie
 

分析:这题转换问法及求去掉最少边使得两个点集不能互通,源点与2连上一条边,1与汇点连上一条边,容量都为无穷,格子间连上容量为1的边,求最大流及为答案。。。数组开小再次wa了一次

代码:

#include<cstdio>using namespace std;const int mm=444444;const int mn=44444;const int oo=1000000000;const int dx[4]={0,1,0,-1};const int dy[4]={1,0,-1,0};int node,src,dest,edge;int reach[mm],flow[mm],next[mm];int head[mn],work[mn],dis[mn],q[mn];inline int min(int a,int b){    return a<b?a:b;}inline 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;}inline void addedge(int u,int v,int c){    reach[edge]=v,flow[edge]=c,next[edge]=head[u],head[u]=edge++;    reach[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=reach[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=reach[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;}int main(){    int i,j,k,x,y,c,n,m,t=0;    while(scanf("%d%d",&n,&m)!=-1)    {        prepare(n*m+2,0,n*m+1);        for(i=0;i<n;++i)            for(j=1;j<=m;++j)            {                scanf("%d",&c);                if(c==1)addedge(i*m+j,dest,oo);                if(c==2)addedge(src,i*m+j,oo);                for(k=0;k<4;++k)                {                    x=i+dx[k];                    y=j+dy[k];                    if(x>=0&&x<n&&y>0&&y<=m)addedge(i*m+j,x*m+y,1);                }            }        printf("Case %d:\n%d\n",++t,Dinic_flow());    }    return 0;}


原创粉丝点击