挺简单一个题,最小割模板

我的感觉就是可能建图的时候会比较麻烦吧,毕竟三个方向。

#include <cctype>
#include <climits>
#include <cstdio>
#include <cstring>
#include <iostream> #define debug(x) std::cout << #x << " = " << x << std::endl; #define __OPTIMIZED__INPUT__
int nextInt()
{
int num = 0;
char c;
bool flag = false;
while ((c = std::getchar()) == ' ' || c == '\r' || c == '\n' || c == '\t');
if (c == '-')
flag = true;
else
num = c - 48;
while (std::isdigit(c = std::getchar()))
num = num * 10 + c - 48;
return (flag ? - 1 : 1) * num;
} struct
{
int to;
int nex;
int v;
} e[600001];
int head[600001];
int h[600001], q[600001];
int ans, n, m; void Insert(const int u, const int v, const int w)
{
static int tot = 0;
tot++;
e[tot].to = v;
e[tot].v = w;
e[tot].nex = head[u];
head[u] = tot;
} bool bfs()
{
int now, i;
std::memset(h, 0xff, sizeof h);
int t = 0;
int w = 1;
q[t] = 1;
h[1] = 0;
while (t < w)
{
now = q[t];
t++;
i = head[now];
for (int i = head[now]; i; i = e[i].nex)
if (e[i].v && h[e[i].to] < 0)
{
q[w++] = e[i].to;
h[e[i].to] = h[now] + 1;
}
}
if (h[n * m] == -1)
return 0;
return 1;
} int dfs(const int x, const int f)
{
if (x == n * m)
return f;
int w, used = 0;
for (int i = head[x]; i; i = e[i].nex)
if (e[i].v && h[e[i].to] == h[x] + 1)
{
w = dfs(e[i].to, std::min(w, e[i].v));
e[i].v -= w;
e[i + 1].v += w;
used += w;
if (used == f)
return f;
}
if (!used)
h[x] = -1;
// debug(used);
return used;
} void Dinic()
{
while (bfs())
ans += dfs(1, INT_MAX);
} int main(int argc, char ** argv)
{
n = nextInt();
m = nextInt();
int x;
for (int i = 1; i <= n; i++)
for (int j = 1; j < m; j++)
{
x = nextInt();
Insert(m * (i - 1) + j, m * (i - 1) + j + 1, x);
Insert(m * (i - 1) + j + 1, m * (i - 1) + j, x);
}
for (int i = 1; i < n; i++)
for (int j = 1; j <= m; j++)
{
x = nextInt();
Insert(m * (i - 1) + j, m * i + j, x);
Insert(m * i + j, m * (i - 1) + j, x);
}
for (int i = 1; i < n; i++)
for (int j = 1; j < m; j++)
{
x = nextInt();
Insert(m * (i - 1) + j, m * i + j + 1, x);
Insert(m * i + j + 1, m * (i - 1) + j, x);
}
Dinic();
std::cout << ans/* << std::endl*/;
#ifdef __NOTEPADPP
std::cin.get();
std::cin.get();
#endif
return 0;
}

【BZOJ1001】[BeiJing2006]狼抓兔子的更多相关文章

  1. BZOJ1001 BeiJing2006 狼抓兔子 【网络流-最小割】*

    BZOJ1001 BeiJing2006 狼抓兔子 Description 现在小朋友们最喜欢的"喜羊羊与灰太狼",话说灰太狼抓羊不到,但抓兔子还是比较在行的,而且现在的兔子还比较 ...

  2. [BZOJ1001][BeiJing2006]狼抓兔子(最小割转最短路|平面图转对偶图)

    1001: [BeiJing2006]狼抓兔子 Time Limit: 15 Sec  Memory Limit: 162 MBSubmit: 31805  Solved: 8494[Submit][ ...

  3. BZOJ1001: [BeiJing2006]狼抓兔子 [最小割 | 对偶图+spfa]

    1001: [BeiJing2006]狼抓兔子 Time Limit: 15 Sec  Memory Limit: 162 MBSubmit: 19528  Solved: 4818[Submit][ ...

  4. BZOJ1001: [BeiJing2006]狼抓兔子【最短路+对偶图】

    题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1001 1001: [BeiJing2006]狼抓兔子 Time Limit: 15 Se ...

  5. bzoj1001: [BeiJing2006]狼抓兔子 -- 最小割

    1001: [BeiJing2006]狼抓兔子 Time Limit: 15 Sec  Memory Limit: 162 MB Description 现在小朋友们最喜欢的"喜羊羊与灰太狼 ...

  6. bzoj1001: [BeiJing2006]狼抓兔子(初识是你最小割)

    1001: [BeiJing2006]狼抓兔子 题目:传送门 题解: 听说这题当初是大难题...可惜当年没有网络流hahahha 现在用网络流的思想就很容易解决了嘛 给什么连什么,注意是双向边,然后跑 ...

  7. BZOJ1001: [BeiJing2006]狼抓兔子(优化的dinic或转化对偶图求最短路)

    1001: [BeiJing2006]狼抓兔子 Time Limit: 15 Sec  Memory Limit: 162 MBSubmit: 30078  Solved: 7908[Submit][ ...

  8. [bzoj1001][BeiJing2006]狼抓兔子_网络流_最小割转对偶图

    狼抓兔子 bzoj-1001 BeiJing2006 Description 现在小朋友们最喜欢的"喜羊羊与灰太狼",话说灰太狼抓羊不到,但抓兔子还是比较在行的, 而且现在的兔子还 ...

  9. BZOJ1001[BeiJing2006]狼抓兔子最小割網絡流

    Description 现在小朋友们最喜欢的"喜羊羊与灰太狼",话说灰太狼抓羊不到,但抓兔子还是比较在行的, 而且现在的兔子还比较笨,它们只有两个窝,现在你做为狼王,面对下面这样一 ...

  10. Bzoj1001 [BeiJing2006]狼抓兔子

    Time Limit: 15 Sec  Memory Limit: 162 MBSubmit: 19759  Solved: 4883 Description 现在小朋友们最喜欢的"喜羊羊与 ...

随机推荐

  1. Android开发之NavigationView的使用

    NavigationView主要是和DrawerLayout框架结合使用,来完成抽屉导航实现侧边栏 引用一段官方文档的示例代码 <android.support.v4.widget.Drawer ...

  2. java学习笔记----java入门

    java基础 一.java语言跨平台原理 1.什么是跨平台? 跨平台就是一个软件可以在不同的操作系统中运行,但是不需要对其修改.换句话说,java语言编写的软件在不做修改的情况下就能在不同的系统平台上 ...

  3. 解决在eclipse中写ImageView时有警告的问题

    Eclipse中写了一个android程序其中main.xml中ImageView哪行是个黄叹号!不知道为什么? 解决办法: android:contentDescription="@str ...

  4. iOS开发之数据存储之XML属性列表(plist)归档

    1.概述 “归档”意思是持久化存储数据.plist文件是一种XML格式的文件,拓展名为plist.如果对象是NSString.NSDictionary.NSArray.NSData.NSNumber等 ...

  5. 【iOS】7.4 定位服务->3.1 地图框架MapKit 功能1:地图展示

    > 本文并非最终版本,如果想要关注更新或更正的内容请关注文集,联系方式详见文末,如有疏忽和遗漏,欢迎指正. --- > 本文相关目录: ================== 所属文集:[[ ...

  6. Recurrent Neural Network系列4--利用Python,Theano实现GRU或LSTM

    yi作者:zhbzz2007 出处:http://www.cnblogs.com/zhbzz2007 欢迎转载,也请保留这段声明.谢谢! 本文翻译自 RECURRENT NEURAL NETWORK ...

  7. python操作数据库之批量导入

    python操作数据库之批量导入 Python语法简洁清晰,特色之一是强制用空白符(white space)作为语句缩进. Python具有丰富和强大的库.它常被昵称为胶水语言,能够把用其他语言制作的 ...

  8. POJ1275出纳员的雇佣【差分约束】

    出纳员的雇佣 Tehran的一家每天24小时营业的超市,需要一批出纳员来满足它的需要.超市经理雇佣你来帮他解决问题:超市在每天的不同时段需要不同数目的出纳员(例如:午夜时只需一小批,而下午则需要很多) ...

  9. PPT自动载入图片并矩阵分布

    最近有学生问到,能不能快速的向PPT一个页面里插入成百张图片,并让它们按统一大小的矩形排布到页面上.我写了以下代码可以在第1页中按照指定横向和纵向矩形数目,填充指定路径下的图片. Sub LoadPi ...

  10. js遍历(获取)ul中的li

    <!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8&qu ...