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

Source: Internet
Author: User

Time Limit:10 Sec Memory limit:162 MB
submit:4017 solved:2037
[Submit] [Status] [Discuss] Description"Wolves love the Sheep Ah love crazy, who let them really love A, wolf love sheep ah is not absurd, they say there is love there is direction ... "Orez heard this song, thought: Wolf and sheep so harmonious, why not try the sheep and wolves together?" Do what 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≤100began to think is the biggest dyeing and other problems, find out what is actually the maximum flow minimum cutfence problem can be converted to, there are two sets, there is no path between, just meet the definition of minimum cutstart building the mapfrom the source point S to the Wolf, the capacity is INFfrom the sheep to the meeting point T, the capacity is INFfrom the wolf to the neighboring sheep and the open space, the capacity of 1from the open space to the adjacent sheep and open space, the capacity of 1run one side maximum flow, code unexpectedly concise
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <queue>5 using namespacestd;6 7 #defineGet (I,j) ((i-1) *m+j)8 Const intinf=0x7f7f7f7f;9 Const intmaxn=400000;Ten Const intxx[4]={-1,1,0,0},yy[4]={0,0,-1,1}; One  A structEdge - { -     intTo,w,next; the }E[MAXN]; - intNode=1, head[maxn],dis[maxn],mp[ $][ $]; - ints=0, t=10001; - intN,m,ans; +  - voidInsertintUintVintW) + { Ae[++node]=(Edge) {v,w,head[u]}; athead[u]=node; -E[++node]= (Edge) {u,0, Head[v]}; -head[v]=node; - } -  - BOOLBFS () in { -memset (dis,-1,sizeof(DIS)); toqueue<int>Q; + Q.push (s); -dis[s]=0; the      while(!q.empty ()) *     { $         intq=Q.front (); Q.pop ();Panax Notoginseng          for(intI=head[q];i;i=e[i].next) -             if(e[i].w&&dis[e[i].to]==-1) the             { + Q.push (e[i].to); Adis[e[i].to]=dis[q]+1; the             } +     } -     returndis[t]!=-1; $ } $  - intDfsintXintflow) - { the     if(x==t)returnflow; -     intW,used=0;Wuyi      for(intI=head[x];i;i=e[i].next) the         if(e[i].w&&dis[e[i].to]==dis[x]+1) -         { Wuw=flow-used; -w=Dfs (E[i].to,min (W,E[I].W)); Aboute[i].w-=W; $e[i^1].w+=W; -used+=W; -             if(Used==flow)returnflow; -         } A     if(!used) dis[x]=-1; +     returnused; the } -  $ voiddinic () the { the      while(BFS ()) ans+=DFS (s,inf); the } the  - intMain () in { thescanf"%d%d",&n,&m); the      for(intI=1; i<=n;i++) About          for(intj=1; j<=m;j++) the         { thescanf"%d",&mp[i][j]); the             if(mp[i][j]==1) Insert (S,Get(i,j), INF); +             Else if(mp[i][j]==2) Insert (Get(i,j), t,inf); -         } the      for(intI=1; i<=n;i++)Bayi          for(intj=1; j<=m;j++) the              for(intk=0;k<4; k++) the             { -                 intx=i+xx[k],y=j+Yy[k]; -                 if(x<1|| x>n| | y<1|| Y&GT;M)Continue; the                 if(mp[i][j]!=1|| mp[x][y]!=1) theInsertGet(I,J),Get(x, Y),1); the             } the dinic (); -printf"%d", ans); the     return 0; the}

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

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.