【网络流】HDU3046 Pleasant sheep and big big wolf (最小割/最大流)

来源:互联网 发布:知乎肿瘤 编辑:程序博客网 时间:2024/05/23 00:42

Pleasant sheep and big big wolf

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2940    Accepted Submission(s): 1207


Problem Description
In ZJNU, there is a well-known prairie. And it attracts pleasant sheep and his companions to have a holiday. Big big wolf and his families know about this, and quietly hid in the big lawn. As ZJNU ACM/ICPC team, we have an obligation to protect pleasant sheep and his companions to free from being disturbed by big big wolf. We decided to build a number of unit fence whose length is 1. Any wolf and sheep can not cross the fence. Of course, one grid can only contain an animal.
Now, we ask to place the minimum fences to let pleasant sheep and his Companions to free from being disturbed by big big wolf and his companions. 

 

Input
There are many cases. 
For every case: 

N and M(N,M<=200)
then N*M matrix: 
0 is empty, and 1 is pleasant sheep and his companions, 2 is big big wolf and his companions.
 

Output
For every case:

First line output “Case p:”, p is the p-th case; 
The second line is the answer. 
 

Sample Input
4 61 0 0 1 0 00 1 1 0 0 02 0 0 0 0 00 2 0 1 1 0
 

Sample Output
Case 1:4

题意:把狼和羊隔开需要的最少栏杆

最小割是对于给定网络为了保证没有从s到t的路径需要删去边的总容量的最小值,此题即最小割,因为最大流等于最小割所以求最大流即可,

此题是多个源点和汇点的情况,只需要增加一个超级源点s和超级汇点t,从s向各个1所在位置连一条容量为INF的边,从每个2所在位置向t连一条容量为INF的边,图上各个位置连上容量为1的边,

code:

#include<stdio.h>#include<string.h>#include<vector>#include<queue>#include<algorithm>#define MAX 20000#define INF 0x3f3f3f3fusing namespace std;struct edge{    int to,cap,rev;};vector<edge> G[MAX];int level[MAX];int iter[MAX];void add_edge(int from,int to,int cap){    G[from].push_back((edge){to,cap,G[to].size()});    G[to].push_back((edge){from,0,G[from].size()-1});}void bfs(int s){    memset(level,-1,sizeof(level));    queue<int>que;    level[s]=0;    que.push(s);    while(!que.empty())    {        int v=que.front();        que.pop();        for(int i=0; i<G[v].size(); i++)        {            edge &e=G[v][i];            if(e.cap>0&&level[e.to]<0)            {                level[e.to]=level[v]+1;                que.push(e.to);            }        }    }}int dfs(int v,int t,int f){    if(v==t)        return f;    for(int &i=iter[v]; i<G[v].size(); i++)    {        edge &e=G[v][i];        if(level[v]<level[e.to]&&e.cap>0)        {            int d=dfs(e.to,t,min(f,e.cap));            if(d>0)            {                e.cap-=d;                G[e.to][e.rev].cap+=d;                return d;            }        }    }    return 0;}int max_flow(int s,int t){    int flow=0;    for(;;)    {        bfs(s);        if(level[t]<0)            return flow;        memset(iter,0,sizeof(iter));        int f;        while((f=dfs(s,t,INF))>0)        {            flow+=f;        }    }}int main(){    int n,m,T=0,a;    while(~scanf("%d%d",&n,&m))    {        int temp;        memset(G,0,sizeof(G));        for(int i=1; i<=n; i++)        {            for(int j=1; j<=m; j++)            {                if(i!=1)                    add_edge((i-1)*m+j,(i-2)*m+j,1);                if(i!=n)                    add_edge((i-1)*m+j,i*m+j,1);                if(j!=1)                    add_edge((i-1)*m+j,(i-1)*m+j-1,1);                if(j!=m)                    add_edge((i-1)*m+j,(i-1)*m+j+1,1);                scanf("%d",&a);                if(a==1)                    add_edge(0,j+(i-1)*m,INF);                else if(a==2)                {                    add_edge(j+(i-1)*m,n*m+1,INF);                }            }        }        printf("Case %d:\n%d\n",++T,max_flow(0,m*n+1));    }}



0 0