http://acm.hdu.edu.cn/showproblem.php?pid=3046

题意:

给出矩阵地图和羊和狼的位置,求至少需要建多少栅栏,使得狼不能到达羊。

思路:
狼和羊不能到达,最小割最大流问题。

因为狼和羊都有多只,所以我们加一个超级源点和一个超级汇点,将每只狼与超级源点相连,容量为INF,将每只羊与超级汇点相连,容量为INF。对于地图上的点,每个点都与它上下左右相连,容量设为1。

接下来,我们只需要计算出从超级源点到超级汇点的最大流,因为最小割等于最大流。

 #include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<vector>
#include<queue>
using namespace std; const int maxn = *;
const int INF = 0x3f3f3f3f; struct Edge
{
int from,to, cap, flow;
Edge(int u, int v, int c, int f) :from(u), to(v), cap(c), flow(f){}
}; int n, m,t;
vector<Edge> edges;
vector<int> G[maxn];
int vis[maxn];
int d[maxn]; //从起点到i的距离
int cur[maxn]; //当前弧下标
int flow; void init()
{
for (int i = ; i < maxn; i++)
G[i].clear();
edges.clear();
} void AddEdge(int from, int to, int cap)
{
edges.push_back(Edge(from, to, cap, ));
edges.push_back(Edge(to, from, , ));
int m = edges.size();
G[from].push_back(m - );
G[to].push_back(m - );
} int BFS()
{
memset(vis, , sizeof(vis));
queue<int> Q;
Q.push();
d[] = ;
vis[] = ;
while (!Q.empty())
{
int x = Q.front();
Q.pop();
for (int i = ; i < G[x].size(); i++)
{
Edge& e = edges[G[x][i]];
if (!vis[e.to] && e.cap>e.flow)
{
vis[e.to] = ;
d[e.to] = d[x] + ;
Q.push(e.to);
}
}
}
return vis[n*m + ];
} int DFS(int x,int a)
{
if (x == n*m + || a == ) return a;
int flow = , f;
for (int& i = cur[x]; i < G[x].size(); i++)
{
Edge& e = edges[G[x][i]];
if (d[x] + == d[e.to] && (f = DFS(e.to, min(a, e.cap - e.flow)))>)
{
e.flow += f;
edges[G[x][i] ^ ].flow -= f;
flow += f;
a -= f;
if (a == ) break;
}
}
return flow;
} void Maxflow(int s,int t)
{
flow = ;
while (BFS())
{
memset(cur, , sizeof(cur));
flow += DFS(s, INF);
}
} int main()
{
//freopen("D:\\txt.txt", "r", stdin);
int kase = ;
int a;
while (~scanf("%d%d", &n, &m))
{
init();
for (int i = ; i <= n;i++)
for (int j = ; j <= m; j++)
{
if (i != )
AddEdge((i - )*m + j, (i-)*m+j, );
if (i != n)
AddEdge((i - )*m + j, i*m + j, );
if (j != )
AddEdge((i - )*m + j, (i - )*m + j - , );
if (j != m)
AddEdge((i - )*m + j, (i - )*m + j + , );
scanf("%d", &a);
if (a == )
AddEdge(, (i - )*m + j, INF);
else if (a == )
AddEdge((i - )*m + j, m*n + , INF);
}
Maxflow(, n*m + );
printf("Case %d:\n%d\n", ++kase, flow);
}
}

HDU 3046 Pleasant sheep and big wolf(最小割最大流+Dinic)的更多相关文章

  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 最小割

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3046 In ZJNU, there is a well-known prairie. And it a ...

  3. 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 ...

  4. UVa 1660 电视网络(点连通度+最小割最大流+Dinic)

    https://vjudge.net/problem/UVA-1660 题意:给出一个无向图,求出点连通度.即最少删除多少个点,使得图不连通. 思路: 如果求线连通度的话,直接求个最大流就可以了.但这 ...

  5. hdu4289 最小割最大流 (拆点最大流)

    最小割最大流定理:(参考刘汝佳p369)增广路算法结束时,令已标号结点(a[u]>0的结点)集合为S,其他结点集合为T=V-S,则(S,T)是图的s-t最小割. Problem Descript ...

  6. 【BZOJ-1797】Mincut 最小割 最大流 + Tarjan + 缩点

    1797: [Ahoi2009]Mincut 最小割 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1685  Solved: 724[Submit] ...

  7. BZOJ-1001 狼抓兔子 (最小割-最大流)平面图转对偶图+SPFA

    1001: [BeiJing2006]狼抓兔子 Time Limit: 15 Sec Memory Limit: 162 MB Submit: 14686 Solved: 3513 [Submit][ ...

  8. hdu1569 方格取数(2) 最大点权独立集=总权和-最小点权覆盖集 (最小点权覆盖集=最小割=最大流)

    /** 转自:http://blog.csdn.net/u011498819/article/details/20772147 题目:hdu1569 方格取数(2) 链接:https://vjudge ...

  9. BZOJ1001:狼抓兔子(最小割最大流+vector模板)

    1001: [BeiJing2006]狼抓兔子 Description 现在小朋友们最喜欢的"喜羊羊与灰太狼",话说灰太狼抓羊不到,但抓兔子还是比较在行的,而且现在的兔子还比较笨, ...

随机推荐

  1. 类似CFS程式发布注意事项

    1.CFS 此AP 程式的验证方式为Forms 验证.需要在IIS 中做如下设置(Forms 身份验证,配套的Web.config 中要有对应节点代码,后台代码中要有获取方式) 2.Forms 身份验 ...

  2. awesome go library 库,推荐使用的golang库

    https://github.com/avelino/awesome-go https://github.com/spf13/cobra                        # A Comm ...

  3. AngularJS2.0起步

    ES6工具链 要让Angular2应用跑起来不是件轻松的事,因为它用了太多还不被当前主流浏览器支持的技术.所以,我们需要一个工具链:

  4. [LeetCode] 312. Burst Balloons_hard tag: 区间Dynamic Programming

    Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented by ...

  5. Math.abs(~2018) —— 入群问答题

    这道题的关键点在于对位操作符“~”的理解,以及内部的具体实现(设计到补码) 最后的结果是:2019 参考文章: http://www.w3school.com.cn/js/pro_js_operato ...

  6. 待解决:PDF header signature not found

  7. python 读不同编码的文本,传递一个可选的encoding 参数给open() 函数

    文件的读写操作默认使用系统编码,可以通过调用sys.getdefaultencoding() 来得到.在大多数机器上面都是utf-8 编码.如果你已经知道你要读写的文本是其他编码方式,那么可以通过传递 ...

  8. Python: collections.nametuple()--映射名称到序列元素

    问题:  通过下标访问列表或者元组中元素 answer: collections.namedtuple()通过使用元组对象来解决这个问题 这个函数实际上是一个返回Python中标准元组类型子类的一个工 ...

  9. 安装vscode with springboot

    1.安装jdk8 2.下载vscode,一切按照默认配置完成安装.下载地址:https://code.visualstudio.com 3.安装完成后,运行vscode.如果没有任何反应,在命令行上运 ...

  10. 删除对象的属性 delete的用法

    Javascript的变量 实际上JavaScript中,变量 = 对象属性,这是因为 Javascript 在执行脚本之前会创建一个Global对象,所有的全局变量都是这个Global对象的属性,执 ...