题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3046

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.

题目描述:在一个单位方格边长为1的矩阵中藏着灰太狼和它的同伴,等待着喜羊羊和它的同伴,为了不让喜羊羊和同伴被抓住,我们可以在矩形草坪中设置单位长度为1的栅栏,求最短的栅栏长度。

算法分析:最小割模型。新增源点和汇点分别为from和to,源点到喜羊羊和同伴连边,权值为inf,每只狼到汇点连边,权值为inf,然后每个方格到相邻格子连边,权值为1即可。

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<queue>
#define inf 0x7fffffff
using namespace std;
const int maxn=*+;
int n,m;
struct node
{
int u,flow;
int next;
}edge[maxn*];
int head[maxn],edgenum;
int from,to; void add(int u,int v,int flow)
{
edge[edgenum].u=v ;edge[edgenum].flow=flow;
edge[edgenum].next=head[u];
head[u]=edgenum++; edge[edgenum].u=u ;edge[edgenum].flow=;
edge[edgenum].next=head[v];
head[v]=edgenum++;
} int d[maxn];
int bfs()
{
memset(d,,sizeof(d));
d[from]=;
queue<int> Q;
Q.push(from);
while (!Q.empty())
{
int u=Q.front() ;Q.pop() ;
for (int i=head[u] ;i!=- ;i=edge[i].next)
{
int v=edge[i].u;
if (!d[v] && edge[i].flow>)
{
d[v]=d[u]+;
Q.push(v);
if (v==to) return ;
}
}
}
return ;
} int dfs(int u,int flow)
{
if (u==to || flow==) return flow;
int cap=flow;
for (int i=head[u] ;i!=- ;i=edge[i].next)
{
int v=edge[i].u;
if (d[v]==d[u]+ && edge[i].flow>)
{
int x=dfs(v,min(cap,edge[i].flow));
cap -= x;
edge[i].flow -= x;
edge[i^].flow += x;
if (cap==) return flow;
}
}
return flow-cap;
}
int dinic()
{
int sum=;
while (bfs()) sum += dfs(from,inf);
return sum;
}
int main()
{
int ncase=;
while (scanf("%d%d",&n,&m)!=EOF)
{
memset(head,-,sizeof(head));
edgenum=;
from=n*m+;
to=from+;
int a;
for (int i= ;i<=n ;i++)
{
for (int j= ;j<=m ;j++)
{
scanf("%d",&a);
if (a==)
{
add(from,(i-)*m+j,inf);
}
else if (a==)
{
add((i-)*m+j,to,inf);
}
if (i->=) add((i-)*m+j,(i-)*m+j,);
if (i+<=n) add((i-)*m+j,i*m+j,);
if (j->=) add((i-)*m+j,(i-)*m+j-,);
if (j+<=m) add((i-)*m+j,(i-)*m+j+,);
}
}
printf("Case %d:\n%d\n",ncase++,dinic());
}
return ;
}

hdu 3046 Pleasant sheep and big big wolf 最小割的更多相关文章

  1. HDU 3046 Pleasant sheep and big big wolf(最小割)

    HDU 3046 Pleasant sheep and big big wolf 题目链接 题意:一个n * m平面上,1是羊.2是狼,问最少要多少围墙才干把狼所有围住,每有到达羊的路径 思路:有羊和 ...

  2. HDU 3046 Pleasant sheep and big big wolf

    Pleasant sheep and big big wolf Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged ...

  3. HDU 3046 Pleasant sheep and big wolf(最小割最大流+Dinic)

    http://acm.hdu.edu.cn/showproblem.php?pid=3046 题意: 给出矩阵地图和羊和狼的位置,求至少需要建多少栅栏,使得狼不能到达羊. 思路:狼和羊不能到达,最小割 ...

  4. Pleasant sheep and big big wolf HDU - 3046(最小割)

    Pleasant sheep and big big wolf Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 ...

  5. Pleasant sheep and big big wolf

    pid=3046">点击打开链接 题目:在一个N * M 的矩阵草原上,分布着羊和狼.每一个格子仅仅能存在0或1仅仅动物.如今要用栅栏将全部的狼和羊分开.问怎么放,栅栏数放的最少,求出 ...

  6. hdu-3046-Pleasant sheep and big big wolf(最大流最小割)

    题意: 给出最少栏杆数使狼和羊分离 分析: 将狼与源点连,羊与汇点连,容量都为无穷,将图的各个相邻点连接,容量为1 然后题目就转化成最小去掉多少条边使不连通,即求最小割最大流. // File Nam ...

  7. HDU 6214.Smallest Minimum Cut 最少边数最小割

    Smallest Minimum Cut Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Oth ...

  8. hdu 5294 Tricks Device 最短路建图+最小割

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=5294 Tricks Device Time Limit: 2000/1000 MS (Java/Other ...

  9. HDU 2435 There is a war (网络流-最小割)

    There is a war Problem Description       There is a sea.       There are N islands in the sea.       ...

随机推荐

  1. centos内核优化--转至网络

    A.关闭selinux(可以预防nginx修改根目录后访问出现404或者403) vi /etc/selinux/config SELINUX=disabled B.修改打开最大文件数句柄也就是 so ...

  2. 关于commons-fileupload组件上传文件中文名乱码问题

    java web开发,常用到的文件上传功能,常用的commons-fileupload和commons-io两个jar包.关于如何使用这两个jar来完成文件上传的功能,这里不做详解.使用commons ...

  3. php时间转换unix时间戳

    本文介绍了php编程中unix时间戳转换的小例子,有关php时间转换.php时间戳的实例代码,有需要的朋友参考下. 第一部分,php 时间转换unix 时间戳实现代码. 复制代码代码示例: <? ...

  4. 菜鸟的js学习笔记

    学的越多感觉不会的越多.php+mysql学了点皮毛.知道搞web的是要会js的于是开始结合公司的项目开始学习js之旅(http://www.w3school.com.cn/b.asp) $(docu ...

  5. “Unable to resolve target 'android-9'”的问题

    SDK版本问题! 如果导入时,eclipse的console中提示信息:“Unable to resolve target 'android-9'” 可能的问题就是你安装的SDK版本高于项目的版本!( ...

  6. 往Android SDCard中读写入数据

    一.用Environment (写) 1.API获取sdcard的路径 File path=Environment.getExternalStorageDirectory(); path=new Fi ...

  7. HTML5 的新的表单属性

    本章讲解涉及 <form> 和 <input> 元素的新属性. 新的 form 属性: autocomplete novalidate 新的 input 属性: autocom ...

  8. C# 文件流基本操作步骤

    private void button1_Click(object sender, EventArgs e) { FileStream fs = new FileStream("123.tx ...

  9. jquery.unobtrusive-ajax.js单独的用法

    (插件本身已经减少了人力,如果开始无脑开发,简直就是无能,@Ajax里面哪里帮助类生成的其实就是jquery.unobtrusive的一些特性) jquery.unobtrusive是MVC中出现的j ...

  10. windows下python+flask环境配置详细图文教程

    本帖是本人在安装配置python和flask环境时所用到的资源下载及相关的教程进行了整理罗列,来方便后面的人员,省去搜索的时间.如果你在安装配置是存在问题可留言给我. 首先罗列一下python+fla ...