题意:

给出最少栏杆数使狼和羊分离

分析:

将狼与源点连,羊与汇点连,容量都为无穷,将图的各个相邻点连接,容量为1

然后题目就转化成最小去掉多少条边使不连通,即求最小割最大流.

// File Name: 3046.cpp
// Author: Zlbing
// Created Time: 2013/9/10 20:41:04 #include<iostream>
#include<string>
#include<algorithm>
#include<cstdlib>
#include<cstdio>
#include<set>
#include<map>
#include<vector>
#include<cstring>
#include<stack>
#include<cmath>
#include<queue>
using namespace std;
#define CL(x,v); memset(x,v,sizeof(x));
#define INF 0x3f3f3f3f
#define LL long long
#define REP(i,r,n) for(int i=r;i<=n;i++)
#define RREP(i,n,r) for(int i=n;i>=r;i--)
const int MAXN=*+;
struct Edge{
int from,to,cap,flow;
};
bool cmp(const Edge& a,const Edge& b){
return a.from < b.from || (a.from == b.from && a.to < b.to);
}
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,cap,});//当是无向图时,反向边容量也是cap,有向边时,反向边容量是0
m=edges.size();
G[from].push_back(m-);
G[to].push_back(m-);
}
bool BFS(){
CL(vis,);
queue<int> Q;
Q.push(s);
d[s]=;
vis[s]=;
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[t];
}
int DFS(int x,int a){
if(x==t||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;
}
//当所求流量大于need时就退出,降低时间
int Maxflow(int s,int t,int need){
this->s=s;this->t=t;
int flow=;
while(BFS()){
CL(cur,);
flow+=DFS(s,INF);
if(flow>need)return flow;
}
return flow;
}
//最小割割边
vector<int> Mincut(){
BFS();
vector<int> ans;
for(int i=;i<edges.size();i++){
Edge& e=edges[i];
if(vis[e.from]&&!vis[e.to]&&e.cap>)ans.push_back(i);
}
return ans;
}
void Reduce(){
for(int i = ; i < edges.size(); i++) edges[i].cap -= edges[i].flow;
}
void ClearFlow(){
for(int i = ; i < edges.size(); i++) edges[i].flow = ;
}
};
Dinic solver;
int main()
{
int n,m;
int cas=;
while(~scanf("%d%d",&n,&m))
{
int s=n*m;
int t=n*m+;
int N=n*m+;
solver.init(N);
int a;
for(int i=;i<n;i++)
{
for(int j=;j<m;j++)
{
scanf("%d",&a);
if(i!=)
solver.AddEdge((i-)*m+j,i*m+j,);
if(j!=)
solver.AddEdge(i*m+j-,i*m+j,);
if(a==)
solver.AddEdge(s,i*m+j,INF);
if(a==)
solver.AddEdge(i*m+j,t,INF);
}
}
int ans=solver.Maxflow(s,t,INF);
printf("Case %d:\n",++cas);
printf("%d\n",ans);
}
return ;
}

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

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

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

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

  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. HDU 3046 Pleasant sheep and big wolf(最小割最大流+Dinic)

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

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

  6. Pleasant sheep and big big wolf

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

  7. HDU 3046Pleasant sheep and big big wolf(切最小网络流)

    职务地址:HDU 3046 最小割第一发!事实上也没什么发不发的. ..最小割==最大流.. 入门题,可是第一次入手最小割连入门题都全然没思路... sad..对最小割的本质还是了解的不太清楚.. 这 ...

  8. HDU 3046

    http://acm.hdu.edu.cn/showproblem.php?pid=3046 典型的最小割模型 #include <iostream> #include <cstdi ...

  9. hdu 2952 Counting Sheep

    本题来自:http://acm.hdu.edu.cn/showproblem.php?pid=2952 题意:上下左右4个方向为一群.搜索有几群羊 #include <stdio.h> # ...

随机推荐

  1. iOS View的Frame和bounds之区别,setbounds使用(深入探究)

    前言: 在ios开发中经常遇到两个词Frame和bounds,本文主要阐述Frame和bound的区别,尤其是bound很绕,较难理解. 一.首先,看一下公认的资料: 先看到下面的代码你肯定就明白了一 ...

  2. Java设计模式03:常用设计模式之单例模式(创建型模式)

    1.  Java之单例模式(Singleton Pattern ) 单例模式是一种常见的设计模式,单例模式分三种:懒汉式单例.饿汉式单例.登记式单例三种. 单例模式有一下特点: 1.单例类只能有一个实 ...

  3. 如何自定义echarts主题

    上一篇,选择echarts原有的主题样式,那么如何自定义自己的主题 与选择原有主题类似 1.echarts官网地址http://echarts.baidu.com/echarts2/doc  在工具中 ...

  4. J2EE 读取文件路径

    在J2ee中实现java类读取webcontent/web-inf/config.xml的实现代码 ,其中../config.xml相对于classes的路径 java.net.URL url = t ...

  5. [Redis] C#中使用redis

    C#中使用redis 首先打开Visual Studio建立一个简单的控制台应用程序,我这里暂时使用的VS2013的版本. 然后通过Nuget进行安装Redis常用组件ServiceStack.Red ...

  6. js自定义方法名

    自定义方法名: <script language="javascript" type="text/javascript">window.onload ...

  7. Orace数据库锁表的处理与总结<摘抄与总结一>

    TM锁(表级锁)类型共有5种,分别称为共享锁(S锁).排它锁(X锁).行级共享锁(RS锁).行级排它锁(RX锁).共享行级排它锁(SRX锁) 当Oracle执行DML语句时,系统自动在所要操作的表上申 ...

  8. openURL的使用方法

    openURL的使用方法 openURL的使用方法: view plaincopy to clipboardprint? [[UIApplication sharedApplication] open ...

  9. UITableViewCell 添加长按手势

    UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc] initWithTarget:self actio ...

  10. WPF 自定义滚动条样式

    先看一下效果: 先分析一下滚动条有哪儿几部分组成: 滚动条总共有五部分组成: 两端的箭头按钮,实际类型为RepeatButton Thumb 两端的空白,实际也是RepeatButton 最后就是Th ...