HDU 3046 Pleasant sheep and big big wolf(最小割)

Pleasant sheep and big big wolfTime Limit: 2000/1000 MS (Java/Others)Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2372Accepted Submission(s): 989

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

题意:给一个n*m的巨阵,1:代表羊,2:代表狼。现在要用长为1的 篱笆去隔开狼与羊,问最少用多少根篱笆。

解题:最小割。建图:源点S=0,汇点T=n*m+1;狼与源点相连,边容INF,羊与汇点相连,边容INF。其他相邻的点相连,边容为1。

#include<stdio.h>#include<string.h>#include<queue>using namespace std;#define captype intconst int MAXN = 40010; //点的总数const int MAXM = 400010; //边的总数const int INF = 1<<30;struct EDG{int to,next;captype cap;} edg[MAXM];int eid,head[MAXN];int gap[MAXN]; //每种距离(或可认为是高度)点的个数int dis[MAXN]; //每个点到终点eNode 的最短距离int cur[MAXN]; //cur[u] 表示从u点出发可流经 cur[u] 号边void init(){eid=0;memset(head,-1,sizeof(head));}//有向边 三个参数,,无向边4个参数void addEdg(int u,int v,captype c,captype rc=0){edg[eid].to=v; edg[eid].next=head[u];edg[eid].cap=c; head[u]=eid++;edg[eid].to=u; edg[eid].next=head[v];edg[eid].cap=rc; head[v]=eid++;}//预处理eNode点到所有点的最短距离void BFS(int sNode, int eNode){queue<int>q;memset(gap,0,sizeof(gap));memset(dis,-1,sizeof(dis));gap[0]=1;dis[eNode]=0;q.push(eNode);while(!q.empty()){int u=q.front(); q.pop();for(int i=head[u]; i!=-1; i=edg[i].next){int v=edg[i].to;if(dis[v]==-1){dis[v]=dis[u]+1;gap[dis[v]]++;q.push(v);}}}}int S[MAXN]; //路径栈,存的是边的id号captype maxFlow_sap(int sNode,int eNode, int n){ //注意:n为点的总个数,包括源点与汇点BFS(sNode, eNode);//预处理eNode到所有点的最短距离if(dis[sNode]==-1) return 0; //源点到不可到达汇点memcpy(cur,head,sizeof(head));int top=0; //栈顶captype ans=0; //最大流int u=sNode;while(dis[sNode]<n){ //判断从sNode点有没有流向下一个相邻的点if(u==eNode){ //找到一条可增流的路captype Min=INF ;int inser;for(int i=0; i<top; i++) //从这条可增流的路找到最多可增的流量Minif(Min>=edg[S[i]].cap){Min=edg[S[i]].cap;inser=i;}for(int i=0; i<top; i++){edg[S[i]].cap-=Min;edg[S[i]^1].cap+=Min; //可回流的边的流量}ans+=Min;top=inser; //从这条可增流的路中的流量瓶颈 边的上一条边那里是可以再增流的,所以只从断流量瓶颈 边裁断u=edg[S[top]^1].to; //流量瓶颈 边的起始点continue;}bool flag = false; //判断能否从u点出发可往相邻点流int v;for(int i=cur[u]; i!=-1; i=edg[i].next){v=edg[i].to;if(edg[i].cap>0 && dis[u]==dis[v]+1){flag=true;cur[u]=i;break;}}if(flag){S[top++] = cur[u]; //加入一条边u=v;continue;}//如果上面没有找到一个可流的相邻点,则改变出发点u的距离(也可认为是高度)为相邻可流点的最小距离+1int Mind= n;for(int i=head[u]; i!=-1; i=edg[i].next)if(edg[i].cap>0 && Mind>dis[edg[i].to]){Mind=dis[edg[i].to];cur[u]=i;}gap[dis[u]]–;if(gap[dis[u]]==0) return ans; //当dis[u]这种距离的点没有了,也就不可能从源点出发找到一条增广流路径//因为汇点到当前点的距离只有一种,那么从源点到汇点必然经过当前点,然而当前点又没能找到可流向的点,那么必然断流dis[u]=Mind+1;//如果找到一个可流的相邻点,则距离为相邻点距离+1,如果找不到,则为n+1gap[dis[u]]++;if(u!=sNode) u=edg[S[–top]^1].to; //退一条边}return ans;}int main(){int _cas=0,n,m,a;int dir[4][2]={0,1,0,-1,1,0,-1,0};while(scanf("%d%d",&n,&m)>0){int s=0,t=n*m+1;init();for(int i=0; i<n; i++)for(int j=0; j<m; j++){scanf("%d",&a);if(a==2)addEdg(s , i*m+j+1, INF);else if(a==1)addEdg(i*m+j+1,t , INF);int ti,tj;for(int e=0; e<4; e++){ti=i+dir[e][0];tj=j+dir[e][1];if(ti>=0&&ti<n&&tj>=0&&tj<m)addEdg(i*m+j+1,ti*m+tj+1, 1);}}printf("Case %d:\n%d\n",++_cas,maxFlow_sap(s , t ,t+1));}}

版权声明:本文为博主原创文章,未经博主允许不得转载。

我们一路上兴致勃勃地参观,当夕阳西下时,才恋恋不舍地离开。

HDU 3046 Pleasant sheep and big big wolf(最小割)

相关文章:

你感兴趣的文章:

标签云: