职务地址: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(切最小网络流)的更多相关文章

  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

    Pleasant sheep and big big wolf Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged ...

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

  4. POJ 2711 Leapin' Lizards / HDU 2732 Leapin' Lizards / BZOJ 1066 [SCOI2007]蜥蜴(网络流,最大流)

    POJ 2711 Leapin' Lizards / HDU 2732 Leapin' Lizards / BZOJ 1066 [SCOI2007]蜥蜴(网络流,最大流) Description Yo ...

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

  6. HDU 3046 Pleasant sheep and big wolf(最小割最大流+Dinic)

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

  7. HDU 4720 Naive and Silly Muggles (外切圆心)

    Naive and Silly Muggles Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...

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

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

  9. HDU3046_Pleasant sheep and big big wolf

    给一个n*m的数字阵,1表示羊的位置,2表示狼的位置,0表示没有东西,可以通过.在每个格子的4边都可以建立围栏,有围栏的话狼是不能通过的. 现在求最少建立多少围栏能够保证狼无法接触到羊. 题目的模型很 ...

随机推荐

  1. 定义自己的仪表板DashBoard - -kankanstyle

    既然做了奶站软件,需要使用的仪表板,显示质量数据 public class MDashboard extends ImageView { private Bitmap mPointerBitmap; ...

  2. 一个能够自己主动生成静态库,自己主动安装程序的Makefile

    .PHONY:clean install CC=g++ CFLAGS=-Wall -g BIN=libecho.a INCLUDE=echo SRC=src OBJS=Socket.o Rio.o T ...

  3. EasyUI - Draggable 拖动控件

    效果: html代码: <div id="box" style="width: 400px; height: 200px; background-color: #f ...

  4. Sublime Text3 + Golang搭建开发环境

    Sublime Text3 + Golang搭建开发环境 http://blog.csdn.net/aqiang912/article/details/46775409 1.安装git 因为golan ...

  5. mysql 执行计划走索引

    <pre name="code" class="html">mysql> desc AssignClientManager; +------- ...

  6. 2014 Multi-University Training Contest 1 — D. Task

    题目链接:pid=4864">http://acm.hdu.edu.cn/showproblem.php?pid=4864 题目大意: 有N个机器.M个任务. 当中每一个机器有xi,y ...

  7. Lucene.Net 2.3.1开发介绍 —— 四、搜索(一)

    原文:Lucene.Net 2.3.1开发介绍 -- 四.搜索(一) 既然是内容筛选,或者说是搜索引擎,有索引,必然要有搜索.搜索虽然与索引有关,那也只是与索引后的文件有关,和索引的程序是无关的,因此 ...

  8. 这里的*号实际表示就是RAC中所有实例都使用

    您的位置: ITPUB个人空间 » cc59的个人空间 » 日志 发布新日志 我的日志我的足迹我的收藏 unix/linuxHA随笔backup&restoreperformance tuni ...

  9. Cocos2d-X3.0 刨根问底(九)----- 场景切换(TransitionScene)源代码分析

    上一章我们分析了Scene与Layer相关类的源代码,对Cocos2d-x的场景有了初步了解,这章我们来分析一下场景变换TransitionScene源代码. 直接看TransitionScene的定 ...

  10. FFT算法的物理意义

    FFT是离散傅立叶变换的高速算法,能够将一个信号变换到频域.有些信号在时域上是非常难看出什么特征的,可是如果变换到频域之后,就非常easy看出特征了.这就是非常多信号分析採用FFT变换的原因.另外,F ...