您的位置:首页 > 其它

HDU3046——Pleasant sheep and big big wolf(最小割最大流定理)

2013-05-10 10:12 429 查看
Pleasant sheep and big big wolf

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

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 6

1 0 0 1 0 0

0 1 1 0 0 0

2 0 0 0 0 0

0 2 0 1 1 0

Sample Output

Case 1:

4

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

解析:

       1连源点,2连汇点,容量无限,各个格子间连边容量为1,求最小割

        由最小割最大流定理转化为求最大流。。。 

代码:

#include<cstdio>
#include<algorithm>
using namespace std;
#define inf 10000

int n,m;
struct bnode
{
int u,v,c,next;
}edge[400000];
int head[500000];
int l=0;
int d[100000],sumd[100000];
int tol;

void add(int u,int v,int c)
{
edge[l].u=u;
edge[l].v=v;
edge[l].c=c;
edge[l].next=head[u];
head[u]=l++;
edge[l].u=v;
edge[l].v=u;
edge[l].c=0;
edge[l].next=head[v];
head[v]=l++;
}

void read()
{
memset(head,255,sizeof(head));
memset(edge,0,sizeof(edge)); l=0;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
{
int x;
scanf("%d",&x);
if(x==1)add(0,(i-1)*m+j,inf);
else if(x==2)add((i-1)*m+j,n*m+1,inf);
if(i+1<=n)add((i-1)*m+j,(i-1)*m+j+m,1);
if(i-1>=1)add((i-1)*m+j,(i-1)*m+j-m,1);
if(j+1<=m)add((i-1)*m+j,(i-1)*m+j+1,1);
if(j-1>=1)add((i-1)*m+j,(i-1)*m+j-1,1);
}
}

int sap(int u,int flow)
{
if(u==tol)return flow;
int sum=0;
for(int i=head[u];i!=-1;i=edge[i].next)
{
int v=edge[i].v;
if(edge[i].c&&d[u]==d[v]+1)
{
int t=sap(v,min(flow-sum,edge[i].c));
edge[i].c-=t;edge[i^1].c+=t;
sum+=t;
if(sum==flow)return sum;
}
}
if(d[0]>=tol+1)return sum;
sumd[d[u]]--;
if(sumd[d[u]]==0)d[0]=tol+1;
sumd[++d[u]]++;
return sum;
}

void work()
{
memset(d,0,sizeof(d));
memset(sumd,0,sizeof(sumd));
tol=n*m+1;
sumd[0]=tol+1;
int ans=0;
while(d[0]<=tol)
ans+=sap(0,2*inf);
printf("%d\n",ans);

}

int main()
{
freopen("hdu3046.in","r",stdin);
freopen("hdu3046.out","w",stdout);
int cnt=0;
while(scanf("%d%d",&n,&m)!=EOF)
{
printf("Case %d:\n",++cnt);
read();
work();
}
return 0;
}


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: