The story of ZJOI2009 Wolf and Sheep

Source: Internet
Author: User
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 to raise 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. Input

The input data is stored in the text file ws.in.
The 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. Output

The output data is stored in the text file ws.out.
The file contains only an integer ans, which represents the shortest length of the fence. Sample Input

2 2
2 2
1 1 Sample Output

2 Data Constraint

10% of Data n,m≤3
30% of Data n,m≤20
100% of data n,m≤100

The requirement of the

is to separate the wolves and sheep by adding fences,
to transform the model, which is the smallest cut.
each point and the surrounding point four-way,
is equivalent to an edge,
if a fence is built, it is equivalent to cutting off the edge. The
connects all the sheep to the source point,
all wolves and sinks have an edge, and the
point is connected to the point with an edge of 1. The
runs the maximum stream at the end of the line. Code

#include <queue> #include <cstdio> #include <iostream> #include <algorithm> #include < cstring> #include <string.h> #include <cmath> #include <math.h> #include <time.h> #define LL l Ong long #define N 100003 #define M 103 #define DB double #define P putchar #define G getchar #define INF 998244353 using
namespace Std;
Char ch;
    void read (int &n) {n=0;
    Ch=g ();
    while ((ch< ' 0 ' | | ch> ' 9 ') && ch!= '-') ch=g ();
    ll W=1;
    if (ch== '-') w=-1,ch=g ();
    while (' 0 ' <=ch && ch<= ' 9 ') n= (n<<3) + (n<<1) +ch-' 0 ', ch=g ();
N*=w; } int max (int a,int b) {return a>b?a:b;} int min (int a,int b) {return a<b?a:b;} ll ABS (LL x) {return x<0?-x:x;} ll SQR (ll x) {return x*x;} void Write (ll x) {if (x>9) write (X/10);

P (x%10+ ' 0 ');}
int Nxt[n*2],to[n*2],v[n*2],last[n],cur[n],tot;
int Q[n],h[n],s,t,ans;

int a[m][m],n,m,t;
    BOOL BFs () {int head=0,tail=1;
    for (int i=0;i<=t;i++) h[i]=-1; q[0]=s;h[s]=0;
        while (head!=tail) {int now=q[head];head++; for (int i=last[now];i;i=nxt[i]) if (V[i] && h[to[i]]==-1) {H[to[i]]=h[now]
                +1;
            Q[tail++]=to[i];
}} return h[t]!=-1;
    } int dfs (int x,int f) {if (x==t) return F;
    int w,used=0;
            for (int i=cur[x];i;i=nxt[i]) if (h[to[i]]==h[x]+1) {w=f-used;
            W=dfs (To[i],min (w,v[i));
            V[i]-=w;v[i^1]+=w;
            if (V[i]) cur[x]=i;
            Used+=w;
        if (used==f) return F;
    } if (!used) h[x]=-1;
return used;
        } void Dinic () {while (BFS ()) {for (int i=0;i<=t;i++) cur[i]=last[i];
    Ans+=dfs (S,inf);
    }} void Ins (int x,int y,int z) {nxt[++tot]=last[x];
    To[tot]=y;
    V[tot]=z;
Last[x]=tot;

} int get (int x,int y) {return x*m-m+y;}
    int main () {tot=1;read (n); read (m); for (int. i=1;i<=n;i++) for (iNT j=1;j<=m;j++) read (a[i][j]); s=n*m+1;
    t=s+1;
            for (int i=1;i<=n;i++) for (int j=1;j<=m;j++) {t=get (i,j);
            if (a[i][j]==1) ins (S,t,inf), INS (T,s,inf);

            if (a[i][j]==2) ins (T,t,inf), INS (T,t,inf);
            if (i!=n) Ins (T,get (I+1,J), 1), Ins (Get (I+1,J), t,1);
        if (j!=m) Ins (T,get (i,j+1), 1), Ins (Get (i,j+1), t,1);
    } dinic ();
printf ("%d\n", ans); }

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.