Pleasant sheep and big big wolf

Time Limit: 1000ms
Memory Limit: 32768KB

This problem will be judged on HDU. Original ID: 3046
64-bit integer IO format: %I64d      Java class name: Main

 
 
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

Source

 
解题:最小割。。
 
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#include <stack>
#define LL long long
#define pii pair<int,int>
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = *;
struct arc{
int to,flow,next;
arc(int x = ,int y = ,int z = -){
to = x;
flow = y;
next = z;
}
};
arc e[maxn<<];
int head[maxn],d[maxn],cur[maxn];
int tot,S,T,n,m;
void add(int u,int v,int flow){
e[tot] = arc(v,flow,head[u]);
head[u] = tot++;
e[tot] = arc(u,flow,head[v]);//可以不这么建图。。。。
head[v] = tot++;
}
bool bfs(){
memset(d,-,sizeof(d));
queue<int>q;
q.push(T);
d[T] = ;
while(!q.empty()){
int u = q.front();
q.pop();
for(int i = head[u]; ~i; i = e[i].next){
if(e[i^].flow && d[e[i].to] == -){
d[e[i].to] = d[u] + ;
q.push(e[i].to);
}
}
}
return d[S] > -;
}
int dfs(int u,int low){
if(u == T) return low;
int tmp = ,a;
for(int &i = cur[u]; ~i; i = e[i].next){
if(e[i].flow && d[e[i].to]+==d[u]&&(a=dfs(e[i].to,min(low,e[i].flow)))){
e[i].flow -= a;
e[i^].flow += a;
low -= a;
tmp += a;
if(!low) break;
}
}
if(!tmp) d[u] = -;
return tmp;
}
int dinic(){
int ans = ;
while(bfs()){
memcpy(cur,head,sizeof(head));
ans += dfs(S,INF);
}
return ans;
}
int main() {
int u,v,w,cs = ;
while(~scanf("%d %d",&n,&m)){
memset(head,-,sizeof(head));
S = n*m;
T = n*m+;
for(int i = tot = ; i < n; ++i)
for(int j = ; j < m; ++j){
scanf("%d",&u);
if(i) add(m*(i-)+j,m*i+j,);
if(j) add(m*i+j-,m*i+j,);
if(u == ) add(S,m*i+j,INF);
if(u == ) add(m*i+j,T,INF);
}
printf("Case %d:\n%d\n",cs++,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 最小割

    题目链接: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 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 3046Pleasant sheep and big big wolf(切最小网络流)

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

  7. HDU 3046

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

  8. hdu 2952 Counting Sheep

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

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

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

随机推荐

  1. 51nod 1301 集合异或和(DP)

    因为当\(A<B\)时,会存在在二进制下的一位,满足这一位B的这一位是\(1\),\(A\)的这一位是\(0\). 我们枚举最大的这一位.设为\(x\)吧. 设计状态.\(dp[i][j][1/ ...

  2. BZOJ 3510 首都 (LCT)

    洛谷P4299传送门 题目大意:给你一颗树,边是一条一条连上去的 在连接过程中会存在询问,询问当前节点所在联通块(其实是一颗树)的重心是哪个节点 以及森林中所有树的重心的异或和 在做这道题之前,要先了 ...

  3. Linux下pyftplib简单的脚本

    from pyftpdlib.authorizers import DummyAuthorizer from pyftpdlib.handlers import FTPHandler from pyf ...

  4. 新手学python-Day2-变量和循环判断

    第二天作业: 初探三级菜单,凭现有知识,注意变量可以不声明,但要提前赋值! 此处shuru = '' 可以不写,因为第7行被赋值了,如果只调用shuru不赋值就会报错 shuru = '' sheng ...

  5. JavaScript 的对象继承方式,有几种写法?

    JavaScript 的对象继承方式,有几种写法? 一.对象冒充 其原理如下:构造函数使用 this 关键字给所有属性和方法赋值(即采用类声明的构造函数方式).因为构造函数只是一个函数,所以可使 Pa ...

  6. (转)彻底学会使用epoll(一)——ET模式实现分析

    注:之前写过两篇关于epoll实现的文章,但是感觉懂得了实现原理并不一定会使用,所以又决定写这一系列文章,希望能够对epoll有比较清楚的认识.是请大家转载务必注明出处,算是对我劳动成果的一点点尊重吧 ...

  7. C#-逆变 协变 反射 代码

    首先看一段测试代码,自己写的 class Program { static void Main(string[] args) { man OneMan = new man(); var d = One ...

  8. UIScrollView加入控件,控件距离顶部始终有间距的问题

    今天.特别郁闷.自己定义了一个UIScrollView,然后在它里面加入控件,如UIButton *button = [[UIButton alloc] initWithFrame:CGRectMak ...

  9. Leetcode_num1_Single Number

    好久没有做题啦.从今天開始刷Leetcode的题.希望坚持的时间能长一点. 先从ac率最高的Single Number開始吧. 题目: Given an array of integers, ever ...

  10. mariadb克隆

    oracle有克隆安装,事实上mysql/mariadb相似.仅仅需简单几步就能够直接在异机直接启动. 环境: node01安装完毕的mariadb; node02一个新机器 如今将node01克隆到 ...