Pleasant sheep and big big wolf

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 19 Accepted Submission(s): 14
 
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表示狼,0表示空地,问建最少的篱笆能把狼和羊分离开。

代码:

//篱笆的长度是1,我们假设把狼放在S集合,羊放在T集合,求S,T的最小割就是答案。
//狼连接源点,羊连接汇点,相邻的各点之间连\无向边/。最小割=最大流。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<cmath>
using namespace std;
const int maxn=,inf=0x7fffffff;
struct edge{
int from,to,cap,flow;
edge(int u,int v,int c,int f):from(u),to(v),cap(c),flow(f){}
};
struct dinic{
int n,m,s,t;
vector<edge>edges;
vector<int>g[maxn];
bool vis[maxn];
int d[maxn];
int cur[maxn];
void init(int n){
this->n=n;
for(int i=;i<n;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,,));//反向弧
m=edges.size();
g[from].push_back(m-);
g[to].push_back(m-);
}
bool bfs(){
memset(vis,,sizeof(vis));
queue<int>q;
q.push(s);
d[s]=;
vis[s]=;
while(!q.empty()){
int x=q.front();q.pop();
for(int i=;i<(int)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[t];
}
int dfs(int x,int a){
if(x==t||a==) return a;
int flow=,f;
for(int&i=cur[x];i<(int)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;
}
int maxflow(int s,int t){
this->s=s;this->t=t;
int flow=;
while(bfs()){
memset(cur,,sizeof(cur));
flow+=dfs(s,inf);
}
return flow;
}
}dc;
int main()
{
int n,m,cas=,mp[][];
while(scanf("%d%d",&n,&m)==){
for(int i=;i<=n;i++)
for(int j=;j<=m;j++) scanf("%d",&mp[i][j]);
int s=,t=n*m+;
dc.init(n*m+);
for(int i=;i<=n;i++)
for(int j=;j<=m;j++){
int nu=(i-)*m+j;
if(mp[i][j]==) dc.addedge(s,nu,inf);
if(mp[i][j]==) dc.addedge(nu,t,inf);
if(i>) dc.addedge(nu,nu-m,);
if(i<n) dc.addedge(nu,nu+m,);
if(j>) dc.addedge(nu,nu-,);
if(j<m) dc.addedge(nu,nu+,);
}
printf("Case %d:\n%d\n",++cas,dc.maxflow(s,t));
}
return ;
}

HDU3046 最大流(最小割)的更多相关文章

  1. 最大流-最小割 MAXFLOW-MINCUT ISAP

    简单的叙述就不必了. 对于一个图,我们要找最大流,对于基于增广路径的算法,首先必须要建立反向边. 反向边的正确性: 我努力查找了许多资料,都没有找到理论上关于反向边正确性的证明. 但事实上,我们不难理 ...

  2. 最大流&最小割 - 专题练习

    [例1][hdu5889] - 算法结合(BFS+Dinic) 题意 \(N\)个点\(M\)条路径,每条路径长度为\(1\),敌人从\(M\)节点点要进攻\(1\)节点,敌人总是选择最优路径即最短路 ...

  3. UVa11248 Frequency Hopping(最大流+最小割)

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=33206 [思路] 最大流最小割. 可以确定的是如果不可行需要修改的 ...

  4. matlab练习程序(最大流/最小割)

    学习这个算法是为学习图像处理中的图割算法做准备的. 基本概念: 1.最大流是一个有向图. 2.一个流是最大流,当且仅当它的残余网络中不包括增广路径. 3.最小割就是网络中所有割中值最小的那个割,最小割 ...

  5. 「网络流24题」「LuoguP2774」方格取数问题(最大流 最小割

    Description 在一个有 m*n 个方格的棋盘中,每个方格中有一个正整数.现要从方格中取数,使任意 2 个数所在方格没有公共边,且取出的数的总和最大.试设计一个满足要求的取数算法.对于给定的方 ...

  6. HDU6582 Path【优先队列优化最短路 + dinic最大流 == 最小割】

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6582 来源:2019 Multi-University Training Contest 1 题目大意 ...

  7. ISAP 最大流 最小割 模板

    虽然这道题用最小割没有做出来,但是这个板子还是很棒: #include<stdio.h> #include<math.h> #include<string.h> # ...

  8. Codeforces 965 枚举轮数贪心分糖果 青蛙跳石头最大流=最小割思想 trie启发式合并

    A /*#include<cstring>#include<algorithm>#include<queue>#include<vector>#incl ...

  9. 网络流 最大流—最小割 之SAP算法 详解

    首先引入几个新名词: 1.距离标号: 所谓距离标号 ,就是某个点到汇点的最少的弧的数量(即边权值为1时某个点到汇点的最短路径长度). 设点i的标号为level[i],那么如果将满足level[i]=l ...

  10. nyoj-677-最大流最小割

    677-碟战 内存限制:64MB 时间限制:2000ms 特判: No通过数:2 提交数:2 难度:4 题目描述: 知己知彼,百战不殆!在战争中如果被敌人掌握了自己的机密,失败是必然的.K国在一场战争 ...

随机推荐

  1. Multi-task Correlation Particle Filter for Robust Object Tracking--论文随笔

    摘要:在这篇论文中,作者提出一种鲁棒视觉跟踪的多任务相关粒子滤波琪跟踪算法(MCPF).作者首先向我们展示了多任务相关滤波器,该滤波器在训练滤波器模板的时候可以学习不同特征之间的联系.本文提出的MCP ...

  2. 90 [LeetCode] Subsets2

    Given a collection of integers that might contain duplicates, nums, return all possible subsets (the ...

  3. Hadoop学习(一):完全分布式集群环境搭建

    1. 设置免密登录 (1) 新建普通用户hadoop:useradd hadoop(2) 在主节点master上生成密钥对,执行命令ssh-keygen -t rsa便会在home文件夹下生成 .ss ...

  4. nginx 添加的配置信息

    使用logrotate管理Nginx日志配置如下: [root@vm-10-129-93-51 nginx]# vi /etc/logrotate.d/nginx /letv/log/nginx/*. ...

  5. c# 调取 c++ dll____c#调用dll

    1.以海康摄像头dll为例.(文章转载https://www.cnblogs.com/smartsensor/p/4343744.html) 海康SDK编程指南 目前使用的海康SDK包括IPC_SDK ...

  6. iOS-AFNetworking与ASIHTTPRequest的区别

    一.底层实现 1.AFN的底层实现基于OC的NSURLConnection和NSURLSession  2.ASI的底层实现基于纯C语言的CFNetwork框架  3.因为NSURLConnectio ...

  7. 分布式系统理论-terms

    Distributed programming is the art of solving the same problem that you can solve on a single comput ...

  8. mysql通过binlog恢复数据

    如果mysql不小心操作失误导致数据错误或者丢失这时候binlog起到了很大的作用 恢复有几种方式 1.按时间恢复--start-datetime   如果确定了时间点,那么按时间恢复是一个再好不过的 ...

  9. [C/C++] C++类对象创建问题

    CSomething a();// 没有创建对象,这里不是使用默认构造函数,而是定义了一个函数,在C++ Primer393页中有说明. CSomething b(2);//使用一个参数的构造函数,创 ...

  10. C++除法运算 // 静态断言

    1.C++中"/"运算:对两个整数做除法,结果仍为整数,如果它的商包含小数部分,则小树部分会被截除. C++ Primer 第五章 P130 2.静态断言(static_asser ...