Each fence is actually a side, repair some fences, so that the wolf can not catch the sheep, in fact, is to seek a cut, so that the sheep all in S, the wolves are all in T.
1#include <cstdio>2#include <cstring>3#include <vector>4#include <queue>5 #defineMAXN 400106 #defineOO 0x3f3f3f3f7 #defineCLR (arr,n) memset (&arr,0,sizeof (arr[0]) * (n+1))8 using namespacestd;9 Ten structEdge { One intu, V, F; AEdge (intUintVintf): U (U), V (v), F (f) {} - }; - structDinic { the intN, SRC, DST; -Vector<edge>Edge; -vector<int>G[MAXN]; - intDEP[MAXN], CUR[MAXN]; + - voidInitintNintSrcintDST) { + This->n =N; A This->SRC =src; at This->DST =DST; - for(intu=1; u<=n; u++ ) - g[u].clear (); - edge.clear (); - } - voidAdd_edge (intUintVintf) { in G[u].push_back (Edge.size ()); - Edge.push_back (Edge (u,v,f)); to G[v].push_back (Edge.size ()); +Edge.push_back (Edge (V,u,0) ); - } the BOOLBFs () { *queue<int>Qu; $ CLR (DEP, N);Panax Notoginseng Qu.push (SRC); -DEP[SRC] =1; the while( !Qu.empty ()) { + intu=Qu.front (); A Qu.pop (); the for(intt=0; T<g[u].size (); t++ ) { +Edge &e=Edge[g[u][t]]; - if(E.F &&!)DEP[E.V]) { $DEP[E.V] = dep[e.u]+1; $ Qu.push (E.V); - } - } the } - returnDEP[DST];Wuyi } the intDfsintUinta) { - if(U==DST | | a==0)returnA; Wu intRemain=a, past=0, na; - for(int&t=cur[u]; T<g[u].size (); t++ ) { AboutEdge &e =Edge[g[u][t]]; $Edge &ve = edge[g[u][t]^1]; - if(E.f && dep[e.v]==dep[e.u]+1&& (na=Dfs (E.v,min (e.f,remain)))) { -Remain-=na; -Past + =na; AE.F-=na; +Ve.f + =na; the if(remain==0) Break; - } $ } the returnpast; the } the intMaxflow () { the intFlow =0; - while(BFS ()) { in CLR (cur, n); theFlow + =DFS (src,oo); the } About returnflow; the } the }; the + intN, M; - intidx[ About][ About], Id_clock; the intmap[ About][ About];Bayi intdx[2] = { +1,0 }; the intdy[2] = {0, +1 }; the dinic D; - - intMain () { the for(intcas=1; ; cas++ ) { the if(SCANF ("%d%d", &n, &m)! =2)return 0; theId_clock =0; the for(intI=1; i<=n; i++ ) - for(intj=1; j<=m; J + + ) { thescanf"%d", &map[i][j]); theIDX[I][J] = + +Id_clock; the }94D.init (id_clock+2, id_clock+1, id_clock+2 ); the for(intI=1; i<=n; i++ ) the for(intj=1; j<=m; J + + ) the for(intD=0; d<2; d++ ) {98 intNI = i+Dx[d]; About intNJ = j+Dy[d]; - if(1<=ni&&ni<=n &&1<=nj&&nj<=m) {101 intU =Idx[i][j];102 intv =Idx[ni][nj];103D.add_edge (U, V,1 );104D.add_edge (V, U,1 ); the }106 }107 for(intI=1; i<=n; i++ )108 for(intj=1; j<=m; J + + ) {109 if(map[i][j]==1 ) the D.add_edge (D.SRC, idx[i][j], oo);111 if(map[i][j]==2 ) the D.add_edge (Idx[i][j], D.DST, oo);113 } theprintf"Case %d:\n%d\n", CAs, D.maxflow ()); the } the}
View Code
HDU 3046 min. cut