HDU 3046Pleasant sheep and big big wolf(切最小网络流)
职务地址:HDU 3046
最小割第一发!事实上也没什么发不发的。
。。最小割==最大流。。
入门题,可是第一次入手最小割连入门题都全然没思路。。。
sad。。对最小割的本质还是了解的不太清楚。。
这题就是对每两个相邻的格子的边界都要进行加边,然后求最大流就OK了。
RE了好长时间,注意遍历加边的时候要从1開始,而不是0開始,由于0是源点的。。
。(或许仅仅有我才犯这样的错误吧。
。
。)建图不多说了。。仅仅要了解了最小割,建图还是非常easy想的。
代码例如以下:
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include <queue>
#include <map>
#include<algorithm>
using namespace std;
const int INF=0x3f3f3f3f;
int head[100000], source, sink, nv, cnt;
int cur[100000], num[100000], d[100000], pre[100000], q[100000], mp[300][300];
struct node
{
int u, v, cap, next;
} edge[10000000];
void add(int u, int v, int cap)
{
edge[cnt].v=v;
edge[cnt].cap=cap;
edge[cnt].next=head[u];
head[u]=cnt++; edge[cnt].v=u;
edge[cnt].cap=0;
edge[cnt].next=head[v];
head[v]=cnt++;
}
void bfs()
{
memset(d,-1,sizeof(d));
memset(num,0,sizeof(num));
int f1=0, f2=0, i;
d[sink]=0;
num[0]=1;
q[f1++]=sink;
while(f1>=f2)
{
int u=q[f2++];
for(i=head[u];i!=-1;i=edge[i].next)
{
int v=edge[i].v;
if(d[v]==-1)
{
d[v]=d[u]+1;
num[d[v]]++;
q[f1++]=v;
}
}
}
}
void isap()
{
memcpy(cur,head,sizeof(cur));
bfs();
int flow=0, u=pre[source]=source, i;
while(d[source]<nv)
{
if(u==sink)
{
int f=INF,pos;
for(i=source;i!=sink;i=edge[cur[i]].v)
{
if(f>edge[cur[i]].cap)
{
f=edge[cur[i]].cap;
pos=i;
}
}
for(i=source;i!=sink;i=edge[cur[i]].v)
{
edge[cur[i]].cap-=f;
edge[cur[i]^1].cap+=f;
}
flow+=f;
u=pos;
}
for(i=cur[u];i!=-1;i=edge[i].next)
{
if(d[edge[i].v]+1==d[u]&&edge[i].cap)
break;
}
if(i!=-1)
{
cur[u]=i;
pre[edge[i].v]=u;
u=edge[i].v;
}
else
{
if(--num[d[u]]==0) break;
int mind=nv;
for(i=head[u];i!=-1;i=edge[i].next)
{
if(mind>d[edge[i].v]&&edge[i].cap)
{
mind=d[edge[i].v];
cur[u]=i;
}
}
d[u]=mind+1;
num[d[u]]++;
u=pre[u];
}
}
printf("%d\n",flow);
}
int main()
{
int n, m, i, j, num=0;
while(scanf("%d%d",&n,&m)!=EOF)
{
num++;
memset(head,-1,sizeof(head));
cnt=0;
source=0;
sink=n*m+1;
nv=sink+1;
for(i=1; i<=n; i++)
{
for(j=1; j<=m; j++)
{
scanf("%d",&mp[i][j]);
if(i!=1)
add((i-1)*m+j,(i-2)*m+j,1);
if(i!=n)
add((i-1)*m+j,i*m+j,1);
if(j!=1)
add((i-1)*m+j,(i-1)*m+j-1,1);
if(j!=m)
add((i-1)*m+j,(i-1)*m+j+1,1);
if(mp[i][j]==1)
add(source,(i-1)*m+j,INF);
else if(mp[i][j]==2)
add((i-1)*m+j,sink,INF);
}
}
printf("Case %d:\n",num);
isap();
}
return 0;
}
版权声明:本文博客原创文章,博客,未经同意,不得转载。
HDU 3046Pleasant sheep and big big wolf(切最小网络流)的更多相关文章
- HDU 3046 Pleasant sheep and big big wolf(最小割)
HDU 3046 Pleasant sheep and big big wolf 题目链接 题意:一个n * m平面上,1是羊.2是狼,问最少要多少围墙才干把狼所有围住,每有到达羊的路径 思路:有羊和 ...
- 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 ...
- 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 ...
- POJ 2711 Leapin' Lizards / HDU 2732 Leapin' Lizards / BZOJ 1066 [SCOI2007]蜥蜴(网络流,最大流)
POJ 2711 Leapin' Lizards / HDU 2732 Leapin' Lizards / BZOJ 1066 [SCOI2007]蜥蜴(网络流,最大流) Description Yo ...
- 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 ...
- HDU 3046 Pleasant sheep and big wolf(最小割最大流+Dinic)
http://acm.hdu.edu.cn/showproblem.php?pid=3046 题意: 给出矩阵地图和羊和狼的位置,求至少需要建多少栅栏,使得狼不能到达羊. 思路:狼和羊不能到达,最小割 ...
- HDU 4720 Naive and Silly Muggles (外切圆心)
Naive and Silly Muggles Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- hdu-3046-Pleasant sheep and big big wolf(最大流最小割)
题意: 给出最少栏杆数使狼和羊分离 分析: 将狼与源点连,羊与汇点连,容量都为无穷,将图的各个相邻点连接,容量为1 然后题目就转化成最小去掉多少条边使不连通,即求最小割最大流. // File Nam ...
- HDU3046_Pleasant sheep and big big wolf
给一个n*m的数字阵,1表示羊的位置,2表示狼的位置,0表示没有东西,可以通过.在每个格子的4边都可以建立围栏,有围栏的话狼是不能通过的. 现在求最少建立多少围栏能够保证狼无法接触到羊. 题目的模型很 ...
随机推荐
- GoldentGate Oracle to Oracle 初始化具体解释
一.安装GoldenGate[源端,目标端] 1.创建ogg文件夹 [root@source ~]# mkdir /DBSoft/ogg [root@source ~]# cd /DBSoft/ogg ...
- delphi实现图象灰度处理的3种方法
灰度处理的方法主要有如下3种: 1.最大值法:使R.G.B的值等于3值中最大的一个,即: R=G=B=max(R,G,B) 最大值法会使形成高亮度很高的灰度图象 var bitmap:tbitma ...
- DELPHI XE7 新的并行库
DELPHI XE7 的新功能列表里面增加了并行库System.Threading, System.SyncObjs. 为什么要增加新的并行库? 还是为了跨平台.以前要并行编程只能从TThread类继 ...
- hdu 4704 同余定理+普通快速幂
此题往后推几步就可找到规律,从1开始,答案分别是1,2,4,8,16.... 这样就可以知道,题目的目的是求2^n%Mod的结果.....此时想,应该会想到快速幂...然后接着会发现,由于n的值过大, ...
- OpenStack使用Bosh部署CloudFoundry(一)—准备OpenStack环境
版本说明: CloudFoundry:V2版本 OpenStack:Folsom或者Grizzly版本 本篇文章采用OpenStack Folsom+nova-network的OpenStack环境, ...
- flask开发restful api
flask开发restful api 如果有几个原因可以让你爱上flask这个极其灵活的库,我想蓝图绝对应该算上一个,部署蓝图以后,你会发现整个程序结构非常清晰,模块之间相互不影响.蓝图对restfu ...
- CURD演示 2
<?php class UserAction extends Action{ public function index(){ echo "你好!"; $m=M('user' ...
- 修改注册表添加IE信任站点及启用Activex控件
Windows Registry Editor Version 5.00 [HKEY_CURRENT_USER/Software/Microsoft/Windows/CurrentVersion/In ...
- android中,如果使用imageButton可以在drawable 中设置一个selector,但是imageView设置不起作用
android中,如果使用imageButton可以在drawable 中设置一个selector,但是imageView设置不起作用,只要把Imageview的src给去掉就成了,src捕获了bac ...
- [Android学习笔记]扩展application
扩展Application对象 每一个应用程序启动之后,都会分配一个linux用户,并且运行在一个独立的进程中.默认情况下,一个应用程序只会运行在一个进程中(可以通过配置android:process ...