hdu 3046 Pleasant sheep and big big wolf(最小割)
2016-07-30 12:36
465 查看
Pleasant sheep and big big wolf
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2757 Accepted Submission(s): 1135
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 6
1 0 0 1 0 0
0 1 1 0 0 0
2 0 0 0 0 0
0 2 0 1 1 0
Sample Output
Case 1:
4
题意:如图所示,输入一个n*m的矩阵,1表示羊,2表示狼,现在修栅栏把狼和羊分隔开,问栅栏最短是多少,一个小正方形的边是1
思路:最小割=最大流,所以我们只要把狼和羊联立起来建图,跑一遍最大流即可
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
using namespace std;
#define N 220
#define INF 99999999
int ma
;
struct Edge
{
int v,next,cap;
}edge[300000];
int dir[4][2]={-1,0,1,0,0,-1,0,1};
int cnt,head[N*N],vis[N*N],d[N*N];
void init()
{
cnt=0;
memset(head,-1,sizeof(head));
}
void addedge(int u,int v,int cap)
{
edge[cnt].v=v; edge[cnt].cap=cap;
edge[cnt].next=head[u];head[u]=cnt++;
edge[cnt].v=u; edge[cnt].cap=0;
edge[cnt].next=head[v];head[v]=cnt++;
}
int bfs(int s,int t,int n)
{
memset(vis,0,sizeof(vis));
memset(d,-1,sizeof(d));
vis[s]=1;
d[s]=0;
queue<int>que;
que.push(s);
while(!que.empty())
{
int u=que.front();
que.pop();
vis[u]=0;
for(int i=head[u];i!=-1;i=edge[i].next)
{
int v=edge[i].v;
if(d[v]==-1&&edge[i].cap>0)
{
d[v]=d[u]+1;
if(!vis[v])
{
vis[v]=1;
que.push(v);
}
}
}
}
return d[t]!=-1;
}
int dfs(int s,int t,int f)
{
if(s==t||f==0) return f;
int flow=0;
for(int i=head[s];i!=-1;i=edge[i].next)
{
int v=edge[i].v;
if(d[v]==d[s]+1&&edge[i].cap>0)
{
int x=min(f-flow,edge[i].cap);
x=dfs(v,t,x);
flow+=x;
if(x)
{
edge[i].cap-=x;
edge[i^1].cap+=x;
}
}
}
if(!flow) d[s]=-2;
return flow;
}
int Dinic(int s,int t,int n)
{
int flow=0,f;
while(bfs(s,t,n))
{
while(f=dfs(s,t,INF))
flow+=f;
}
return flow;
}
int main()
{
int n,m,tot=1;
while(~scanf("%d %d",&n,&m))
{
init();
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
scanf("%d",&ma[i][j]);
int s=0,t=n*m+1;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
for(int k=0;k<4;k++)
{
int x=i+dir[k][0],y=j+dir[k][1];
if(x<1||x>n||y<1||y>m) continue;
addedge((i-1)*m+j,(x-1)*m+y,1);
}
if(ma[i][j]==1)
addedge((i-1)*m+j,t,INF);
else if(ma[i][j]==2)
addedge(s,(i-1)*m+j,INF);
}
}
int ans=Dinic(s,t,t);
printf("Case %d:\n",tot++);
printf("%d\n",ans);
}
return 0;
}
相关文章推荐
- HDU 3046 Pleasant sheep and big big wolf(最小割-Dinic)
- HDU 3046 Pleasant sheep and big big wolf(最小割)
- hdu 3046 Pleasant sheep and big big wolf 最小割
- HDU-3046 Pleasant sheep and big big wolf 网络流,最小割
- HDU 3046 Pleasant sheep and big big wolf(最小割)
- 【HDU - 3046】Pleasant sheep and big big wolf 【最大流-最小割】
- HDU 3046 Pleasant sheep and big big wolf(最小割)
- hdu 3046 Pleasant sheep and big big wolf【最大流Dinic--------最小割】
- hdu 3046 Pleasant sheep and big big wolf (最小割)
- HDU - 3046 Pleasant sheep and big big wolf(最小割)
- HDU 3046 Pleasant sheep and big wolf(最小割最大流+Dinic)
- hdu 3046 Pleasant sheep and big big wolf(最小割)
- hdu 3046 Pleasant sheep and big big wolf 最小割
- hdu-3046-Pleasant sheep and big big wolf(最大流最小割)
- hdu 3046 Pleasant sheep and big big wolf(最小割)
- HDU 3046-Pleasant sheep and big big wolf(网络流_最小割)
- HDU 3046 Pleasant sheep and big big wolf(最小割)
- hdu 3046 Pleasant sheep and big big wolf(最小割)
- HDOJ 3046 - Pleasant sheep and big big wolf 构图最小割
- hdoj 3046 Pleasant sheep and big big wolf 【入门最小割】