"Bzoj" 1412: [ZJOI2009] The story of the Wolf and the Sheep

Source: Internet
Author: User
Description "Wolf Love Sheep Ah love crazy, who let them really love A, wolf love sheep ah not absurd, they say there is love there is direction ... Orez heard this song, thinking: Wolf and sheep so harmonious, why not try the sheep and wolves together. Do as you say. Orez can be seen as a n*m matrix lattice, the edge of the matrix has been put on the fence. But Drake soon found that wolves are also wolves, they are always watering to the sheep, the song is just a moving legend. So Orez decided to add some fences in the Sheep Wolf Circle, or to keep the wolves separately. Through careful observation, Orez found that wolves and sheep have their own territory, if wolves and sheep can not stay in their own territory, then they will become very irritable, not conducive to their growth. Orez want to add the fence as short as possible. Of course, the fence must first be guaranteed not to change the wolf sheep's territory, and then the fence has to be built intact, that is, must be built on the boundaries of the unit lattice and can not only build part. InputThe first line of the file contains two integers n and M. Next n rows of m integers per line, 1 means that the lattice belongs to the wolf's territory, 2 is the territory of the sheep, and 0 means that the lattice is not the territory of any animal. OutputThe file contains only an integer ans, which represents the shortest length of the fence. Sample Input2 2
2 2
1 1

Sample Output2

Data range
10% of Data n,m≤3
30% of Data n,m≤20
100% of Data n,m≤100
The obvious minimum cut excavation fence nature: can only be built in the adjacent two, and built so that there is no path between the wolf and the sheep. The definition of cut is that there is no path to the S-set and T-sets.   The problem is that the minimum number of fences is required. The main is to build the edge from the source point to the infinite edge of the wolf from the sheep to the infinity edge of the meeting point and then the wolf to the open space and the sheep open space and sheep
  1 #include <queue> 2 #include <cstdio> 3 #include <iostream> 4 #define MAXN 100010 5 6 Const I
  7 8 using namespace Std;
 9 int map[101][101]; 
 One-by-one int n,m,st,en,ans;
 int x[4]={0,1,0,-1};
 int y[4]={1,0,-1,0};
 + struct Node {int to;
 int next;
 int Val;
 Node E[MAXN];
 Head[maxn],tot=1 int;
 Queue<int> Q;
 inline void Read (int&x) {int F=1;x=0;char c=getchar (); (c> ' 9 ' | |
 c< ' 0 ') {if (c== '-') F=-1;c=getchar ();}
 while (c>= ' 0 ' &&c<= ' 9 ') {x= (x<<1) + (x<<3) +c-48;c=getchar ();}
 (int x,int y,int val) {e[++tot].to=y;
 () ()-------------"i=0;i<=en;i++" (int) cur[i]=head[i],depth[i]=-1; (!q.empty ())Q.pop ();
 Q.push (ST);d epth[st]=0;
 (!q.empty ()) {U=q.front int ();
 Q.pop ();
 Wuyi for (int i=head[u];i!=0;i=e[i].next) {to=e[i].to int;
 if (e[i].val>0&&depth[to]==-1) {depth[to]=depth[u]+1;
 Q.push (to);
 if (to==en) return true;
 + +}
 Dinic (int now,int flow) {(now==en) return flow;
 Rest=0,delat int;
 (int & i=cur[now];i!=0;i=e[i].next) {to=e[i].to int;
 if (depth[to]==depth[now]+1&&e[i].val>0) {delat=dinic (To,min (e[i].val,flow-rest));
 if (Delat) {E[i].val-=delat;
 if (rest==flow) break; * (Flow!=rest) Depth[now]=-1;
 return rest;
 The "M" Bayi () () () () {read (n);
 i=1;i<=n;i++ for (int) (int.) (int j=1;j<=m;j++) {read (map[i][j]);
 if (map[i][j]==1) Add (St, (i-1) *m+j,inf), add ((i-1) *m+j,st,0);
 (map[i][j]==2) Add ((i-1) *m+j,en,inf), add (en, (i-1) *m+j,0);              (int i=1;i<=n;i++) j=1;j<=m;j++ for (int k=0;k<4;k++) {94}
 int xx=i+x[k];
 yy=j+y[k int]; if (xx<1| | yy<1| | xx>n| | yy>m| |
 map[i][j]==2) continue;
 if (map[xx][yy]!=1) Add ((i-1) *m+j, (xx-1) *m+yy,1), add ((xx-1) *m+yy, (i-1) *m+j,0);
98} (BFS ()) ans+=dinic (St,inf);
-printf ("%d\n", ans);
101 return 0;  102}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.