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

典型的最小割模型

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std ; const int INF=0xfffffff ;
struct node
{
int s,t,cap,nxt ;
}e[] ;
int m,n,cnt,head[],level[],q[] ;
void add(int s,int t,int cap)
{
e[cnt].s=s ;e[cnt].t=t ;e[cnt].cap=cap ;e[cnt].nxt=head[s] ;head[s]=cnt++ ;
e[cnt].s=t ;e[cnt].t=s ;e[cnt].cap= ;e[cnt].nxt=head[t] ;head[t]=cnt++ ;
}
bool build(int s,int t)
{
int front=,rear= ;
memset(level,-,sizeof(level)) ;
q[rear++]=s ;
level[s]= ;
while(front<rear)
{
int u=q[front++] ;
for(int i=head[u] ;i!=- ;i=e[i].nxt)
{
int tt=e[i].t ;
if(level[tt]==- && e[i].cap>)
{
level[tt]=level[u]+ ;
if(tt==t)return true ;
q[rear++]=tt ;
}
}
}
return false ;
}
int find(int s,int t,int flow)
{
if(s==t)return flow ;
int ret=,a ;
for(int i=head[s] ;i!=- ;i=e[i].nxt)
{
int tt=e[i].t ;
if(level[tt]==level[s]+ && e[i].cap>)
{
a=find(tt,t,min(e[i].cap,flow-ret)) ;
e[i].cap-=a ;
e[i^].cap+=a ;
ret+=a ;
if(ret==flow)
return ret ;
}
}
if(!ret)level[s]=- ;
return ret ;
}
int dinic(int s,int t)
{
int flow,ret= ;
while(build(s,t))
while(flow=find(s,t,INF))
ret+=flow ;
return ret ;
}
int Map[][] ;
int main()
{
int N,M ;
int cas= ;
while(~scanf("%d%d",&N,&M))
{
cnt= ;
memset(head,-,sizeof(head)) ;
int S,T ;
for(int i= ;i<=N ;i++)
{
for(int j= ;j<=M ;j++)
{
scanf("%d",&Map[i][j]) ;
}
}
S= ;T=N*M+ ;
for(int i= ;i<=N ;i++)
{
for(int j= ;j<=M ;j++)
{
int num=(i-)*M+j ;
if(i>)add(num,num-M,) ;
if(i<N)add(num,num+M,) ;
if(j>)add(num,num-,) ;
if(j<M)add(num,num+,) ;
if(Map[i][j]==)add(S,num,INF) ;
if(Map[i][j]==)add(num,T,INF) ;
}
}
printf("Case %d:\n%d\n",cas++,dinic(S,T)) ;
}
return ;
}

HDU 3046的更多相关文章

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

  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. hdu 3046 最小割

    每个栅栏其实就是一条边,修一些栅栏,使得狼不能抓到羊,其实就是求一个割,使得羊全在S中,狼全在T中. #include <cstdio> #include <cstring> ...

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

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

  8. Soj题目分类

    -----------------------------最优化问题------------------------------------- ----------------------常规动态规划 ...

  9. Hdu 1214 圆桌会议

    圆桌会议 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

随机推荐

  1. Python 获取文件的创建时间,修改时间和访问时间

    # 用到的知识# os.path.getatime(file) 输出文件访问时间# os.path.getctime(file) 输出文件的创建时间# os.path.getmtime(file) 输 ...

  2. Spring boot 遇到了连接mysql的错误

    问题1: Establishing SSL connection without server's identity verification is not recommended. Accordin ...

  3. TinyXml节点查找及修改

    参考:http://blog.csdn.net/zww0815/article/details/8083550 // 读者对象:对TinyXml有一定了解的人.本文是对TinyXml工具的一些知识点的 ...

  4. vscode python3 配置生成任务

    一直用sublime,但是ubuntu下输入中文有问题,解决起来太麻烦: pycharm太重.虚拟机一开+Chrome打开10几个页面,然后再运行pycharm,静音轻薄笔记本CPU和8G内存基本都占 ...

  5. spring boot: GlobalDefaultExceptionHandler方法内的友好错误提示,全局异常捕获

    spring boot: GlobalDefaultExceptionHandler方法内的友好错误提示,全局异常捕获 当你的某个控制器内的某个方法报错,基本上回显示出java错误代码,非常不友好,这 ...

  6. LeetCode--141--环形链表

    问题描述: 给定一个链表,判断链表中是否有环. 思路:用快的指针追慢的指针,只要有圈,一定能追上. 错误: class Solution(object): def hasCycle(self, hea ...

  7. FastDFS install

    Version: os: centos7 x64 FastDFS: 5.05 libfastcommon: latest 1. dwonload libfastcommon https://githu ...

  8. ubuntu mysql主从库的搭建

    1,首先我们要确定一个从库一个主库,紧记从库只能读取不能有其他的操作,如果操作写那主从就失效了,那就看看我们这么搭建主从吧! 2. 环境:Ubuntu,Mysql (主从的数据库版本必须保持一致) 主 ...

  9. RabbitMQ特性

    使用默认的exchange channel.basicPublish("", QUEUE_NAME, null, message.getBytes()); 如果用空字符串去申明一个 ...

  10. forget word out a~2

        1● an 不,非,无   2● amphi     两个,两种     3● ad 做,加强: