Bzoj 1412 The story of the Wolf and the Sheep

Source: Internet
Author: User

This is the minimum cut, but I think for a long time ....

Consider all sheep connected to the source point, the wolf connected to the meeting point, adjacent points on both sides of the minimum cut (maximum flow) can be.

Correctness? Considering something like a dichotomy, we just need to set the wolf together and waist the sheep.

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#define Maxe 100005
#define MAXV 10011
#define INF 1234567
#define S 0
#define T 10010
using namespace Std;
int n,m,map[105][105],dx[]={0,0,1,0,-1},dy[]={0,1,0,-1,0};
int dis[maxv],g[maxv],nume=1;
struct Edge
{
int v,f,nxt;
}e[maxe];
void Addedge (int u,int v,int flow)
{
E[++nume].v=v;
E[nume].nxt=g[u];
E[nume].f=flow;
G[u]=nume;
E[++nume].v=u;
E[NUME].NXT=G[V];
e[nume].f=0;
G[v]=nume;
}
void Build ()
{
for (int i=1;i<=n;i++)
for (int j=1;j<=m;j++)
{
if (map[i][j]==1) Addedge (S, (i-1) *m+j,inf);
else if (map[i][j]==2) Addedge ((i-1) *m+j,t,inf);
for (int k=1;k<=4;k++)
{
int tx=i+dx[k],ty=j+dy[k];
if ((tx>=1) && (tx<=n) && (ty>=1) && (ty<=m))
Addedge ((i-1) *m+j, (tx-1) *m+ty,1);
}
}
}
BOOL BFs ()
{
memset (dis,-1,sizeof (dis));
Queue <int> q;
Q.push (s);
dis[s]=0;
while (!q.empty ())
{
int Head=q.front ();
Q.pop ();
for (int i=g[head];i;i=e[i].nxt)
{
if ((e[i].f>0) && (dis[e[i].v]<0))
{
dis[e[i].v]=dis[head]+1;
Q.push (E[I].V);
}
}
}
if (dis[t]==-1) return false;
return true;
}
int dinic (int x,int low)
{
if (x==t) return low;
int ret=0;
for (int i=g[x];low && I;I=E[I].NXT)
{
if ((e[i].f!=0) && (dis[e[i].v]==dis[x]+1))
{
int Dd=dinic (E[i].v,min (LOW,E[I].F));
LOW=LOW-DD;
RET=RET+DD;
E[I].F=E[I].F-DD;
E[I^1].F=E[I^1].F+DD;
}
}
if (ret==0) dis[x]=-1;
return ret;
}
int main ()
{
memset (map,0,sizeof (map));
memset (G,0,sizeof (g));
scanf ("%d%d", &n,&m);
for (int i=1;i<=n;i++)
for (int j=1;j<=m;j++)
scanf ("%d", &map[i][j]);
Build ();
int ans=0;
while (BFS () ==true)
Ans=ans+dinic (S,inf);
printf ("%d\n", ans);
return 0;
}

Bzoj 1412 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.