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 集合交补

    setx = set(["apple", "mango"]) sety = set(["mango", "orange" ...

  2. yaml 文件保存

    with open(mpath, "w") as f: yaml.safe_dump(yaml_dict,f,encoding='utf-8', allow_unicode=Tru ...

  3. 部署showdoc

    1.下载 https://github.com/star7th/showdoc 2.解压 sudo tar -zvxf ~/showdoc-2.4.5.tar.gz -C /home/wwwroot/ ...

  4. Codeforces 913D - Too Easy Problems

    913D - Too Easy Problems 思路:二分check k 代码: #include<bits/stdc++.h> using namespace std; #define ...

  5. Codeforces 101628A - Arthur's Language

    101628A - Arthur's Language 思路:dp,状态转移见代码. 代码: #include<bits/stdc++.h> using namespace std; #d ...

  6. Python mysql-SQL概要

    2017-09-05 20:10:58 一.SQL语句及其种类 SQL使用关键字,表名,列名等组合成一条语句来描述操作的内容.关键字是指那些含义或者使用方法是先已经定义好的英语单词.根据RDBMS赋予 ...

  7. template.js 模版内调用外部JS方法

    template.js 一款 JavaScript 模板引擎,简单,好用.提供一套模板语法,用户可以写一个模板区块,每次根据传入的数据,生成对应数据产生的HTML片段,渲染不同的效果.模版定义如下: ...

  8. English trip -- VC(情景课)1 F Another view

    Another view 另一种观点 拓展应用 Life-skills reading  生活技能阅读 Midtown Adult School  中城成人学校 NAME:  Samir Ahmed  ...

  9. php--------返回404状态

    php header()返回404状态代码的两种方式 //方式一 header('HTTP/1.1 404 Not Found');exit('404') //方式二 header("sta ...

  10. Android之RecyclerView实现时光轴

    做项目的过程中有个需求需要时光轴,于是网上找了部分资料 ,写了个案例,现在分享给大家. 如图: activity_main.xml <?xml version="1.0" e ...