HDU 3046 Pleasant sheep and big wolf(最小割最大流+Dinic)
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)的更多相关文章
- HDU 3046 Pleasant sheep and big big wolf(最小割)
		
HDU 3046 Pleasant sheep and big big wolf 题目链接 题意:一个n * m平面上,1是羊.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 ...
 - 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 ...
 - UVa 1660 电视网络(点连通度+最小割最大流+Dinic)
		
https://vjudge.net/problem/UVA-1660 题意:给出一个无向图,求出点连通度.即最少删除多少个点,使得图不连通. 思路: 如果求线连通度的话,直接求个最大流就可以了.但这 ...
 - hdu4289 最小割最大流 (拆点最大流)
		
最小割最大流定理:(参考刘汝佳p369)增广路算法结束时,令已标号结点(a[u]>0的结点)集合为S,其他结点集合为T=V-S,则(S,T)是图的s-t最小割. Problem Descript ...
 - 【BZOJ-1797】Mincut 最小割    最大流 + Tarjan + 缩点
		
1797: [Ahoi2009]Mincut 最小割 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1685 Solved: 724[Submit] ...
 - BZOJ-1001  狼抓兔子  (最小割-最大流)平面图转对偶图+SPFA
		
1001: [BeiJing2006]狼抓兔子 Time Limit: 15 Sec Memory Limit: 162 MB Submit: 14686 Solved: 3513 [Submit][ ...
 - hdu1569 方格取数(2) 最大点权独立集=总权和-最小点权覆盖集 (最小点权覆盖集=最小割=最大流)
		
/** 转自:http://blog.csdn.net/u011498819/article/details/20772147 题目:hdu1569 方格取数(2) 链接:https://vjudge ...
 - BZOJ1001:狼抓兔子(最小割最大流+vector模板)
		
1001: [BeiJing2006]狼抓兔子 Description 现在小朋友们最喜欢的"喜羊羊与灰太狼",话说灰太狼抓羊不到,但抓兔子还是比较在行的,而且现在的兔子还比较笨, ...
 
随机推荐
- 使用java进行excel读取和写入
			
1:添加处理excel的依赖jar包 <!-- 引入poi,解析workbook视图 --> <dependency> <groupId>org.apache.po ...
 - bootstrap modal插件弹出窗口如何限制最大高度,并且在内容过多时可以滚动显示
			
.modal-body{ max-height:400px; overflow-y:auto; } 只有在modal-body类上限制高度才能起作用,其他地方的限制均不起作用
 - Scala系统学习(四):Scala数据类型
			
Scala与Java具有相同的数据类型,具有相同的内存占用和精度.以下是提供Scala中可用的所有数据类型的详细信息的表格: 序号 数据类型 说明 1 Byte 8位有符号值,范围从-128至127 ...
 - HDU5012:Dice(bfs模板)
			
http://acm.hdu.edu.cn/showproblem.php?pid=5012 Problem Description There are 2 special dices on the ...
 - [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 ...
 - zw版【转发·台湾nvp系列Delphi例程】HALCON HistoToThresh1
			
zw版[转发·台湾nvp系列Delphi例程]HALCON HistoToThresh1 procedure TForm1.Button1Click(Sender: TObject);var imag ...
 - Object-C-NSArray
			
NSArray *fruitArray=[[NSArray alloc] initWithObjects:@"apple",@"banana",@"p ...
 - Java缓存学习之五:spring 对缓存的支持
			
(注意标题,Spring对缓存的支持 这里不单单指Ehcache ) 从3.1开始,Spring引入了对Cache的支持.其使用方法和原理都类似于Spring对事务管理的支持.Spring Cache ...
 - python , 顺序迭代合并后的list对象
			
有一系列排序序列,想将它们合并后得到一个排序序列并在上面迭代遍历 heapq.merge() 函数可以帮你解决这个问题.比如: >>> import heapq >>&g ...
 - 在windows上构建LLVM 7.0.1
			
关于在windows上构建LLVM,网上有不少文章,但都是互相抄来的,写作时极不认真,不是少这个,就是少那个,没有一篇是可以完整照着做下来的,实在气人. 本文的安装和配置过程,我亲自操作过好几遍,不惜 ...