Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=3046
In Zjnu, there is a well-known prairie. And it attracts pleasant sheep and his companions to has a holiday. Big Big Wolf and his families know on this, and quietly hid in the big lawn. As Zjnu ACM/ICPC team, we have a obligation to protect pleasant sheep and he 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 for the minimum fences to let pleasant sheep and he companions to free from being disturbed by Big BIG W Olf and his companions.
Title Description: In a flat square side length of 1 of the Matrix hidden ash too wolf and its companion, waiting for the pleasant goat and its companion, in order not to let the pleasant goat and companion was caught, we can set the rectangular lawn in the length of 1 of the fence, the shortest fence length.
Algorithm analysis: Minimum cut model. The new source and sink points are from and to, the source point to the pleasant goat and companion edge, the value of the INF, each wolf to the meeting point of the edge, the value of the INF, and then each square to the adjacent lattice connected edge, the weight of 1.
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <cstdlib>5#include <cmath>6#include <algorithm>7#include <queue>8 #defineINF 0x7fffffff9 using namespacestd;Ten Const intmaxn= $* $+Ten; One intn,m; A structnode - { - intU,flow; the intNext; -}edge[maxn*4]; - intHead[maxn],edgenum; - int from, to; + - voidAddintUintVintflow) + { AEdge[edgenum].u=v; edge[edgenum].flow=flow; atedge[edgenum].next=Head[u]; -head[u]=edgenum++; - -Edge[edgenum].u=u; edge[edgenum].flow=0; -edge[edgenum].next=Head[v]; -head[v]=edgenum++; in } - to intD[MAXN]; + intBFS () - { thememset (D,0,sizeof(d)); *d[ from]=1; $queue<int>Q;Panax NotoginsengQ.push ( from); - while(!q.empty ()) the { + intu=Q.front (); Q.pop (); A for(intI=head[u]; i!=-1; i=edge[i].next) the { + intv=edge[i].u; - if(!d[v] && edge[i].flow>0) $ { $d[v]=d[u]+1; - Q.push (v); - if(v==to)return 1; the } - }Wuyi } the return 0; - } Wu - intDfsintUintflow) About { $ if(U==to | | flow==0)returnflow; - intcap=flow; - for(intI=head[u]; i!=-1; i=edge[i].next) - { A intv=edge[i].u; + if(d[v]==d[u]+1&& edge[i].flow>0) the { - intx=Dfs (V,min (cap,edge[i].flow)); $Cap-=x; theEdge[i].flow-=x; theedge[i^1].flow + =x; the if(cap==0)returnflow; the } - } in returnflow-cap; the } the intdinic () About { the intsum=0; the while(BFS ()) sum + = DFS ( from, INF); the returnsum; + } - intMain () the {Bayi intNcase=1; the while(SCANF ("%d%d", &n,&m)! =EOF) the { -memset (head,-1,sizeof(head)); -edgenum=0; the from=n*m+1; theto= from+1; the intA; the for(intI=1; i<=n; i++.) - { the for(intj=1; j<=m; j + +) the { thescanf"%d",&a);94 if(a==1) the { theAdd from, (I-1) *m+j,inf); the }98 Else if(a==2) About { -Add (I-1) *m+j,to,inf);101 }102 if(I-1>=1) Add ((I-1) *m+j, (I-2) *m+j,1);103 if(i+1<=n) Add ((I-1) *m+j,i*m+j,1);104 if(J-1>=1) Add ((I-1) *m+j, (I-1) *m+j-1,1); the if(j+1<=M) Add ((I-1) *m+j, (I-1) *m+j+1,1);106 }107 }108printf"Case %d:\n%d\n", ncase++, Dinic ());109 } the return 0;111}
HDU 3046 pleasant sheep and Big big Wolf min cut