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

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

#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. Spring 4 支持的 Java 8 特性

    Spring 框架 4 支持 Java 8 语言和 API 功能.在本文中,我们将重点放在 Spring 4 支持新的 Java 8 的功能.最重要的是 Lambda 表达式,方法引用,JSR-310 ...

  2. 降低Redis内存占用

    1.降低redis内存占用的优点 1.有助于减少创建快照和加载快照所用的时间 2.提升载入AOF文件和重写AOF文件时的效率 3.缩短从服务器进行同步所需的时间 4.无需添加额外的硬件就可以让redi ...

  3. FDG内存分配器笔记

    FDG: 大规模并行系统中的动态内存分配器由于需要全局同步(记账) ,导致性能急剧下降. 代码解析 1.superblock 类中包含两个变量,两个函数.默认superblock大小为2048 ite ...

  4. The superclass “javax.servlet.http.HttpServlet" was not found on the Java Build Path错误

    1.异常信息 创建maven web项目时,出现 The superclass "javax.servlet.http.HttpServlet" was not found on ...

  5. Project下载提示检索 COM 类工厂中 CLSID 为 {36D27C48-A1E8-11D3-BA55-00C04F72F325} 的组件失败

    做后台系统导出Project时,部署到服务器提示:检索 COM 类工厂中 CLSID 为 {36D27C48-A1E8-11D3-BA55-00C04F72F325} 的组件失败,原因是出现以下错误: ...

  6. POJ2352Stars【树状数组】

    Stars Description Astronomers often examine star maps where stars are represented by points on a pla ...

  7. 老李分享:《Linux Shell脚本攻略》 要点(七)

    老李分享:<Linux Shell脚本攻略> 要点(七)   1.显示给定文件夹下的文件的磁盘适用情况 [root@localhost program_test]# du -a -h ./ ...

  8. 请不要把‘通知的观察者注册’放在-viewWillAppear:中

    接手项目二次开发的吐槽: 接手别人的代码的悲哀之一就是,我反复的把流程走了一遍又一遍,却始终无法发现原来是这个问题. 之前这个人把通知的观察者注册放在了-viewWillAppear:中,导致,我发送 ...

  9. JAVA加密算法系列-AES

    package ***; import java.io.UnsupportedEncodingException; import java.security.InvalidKeyException; ...

  10. AngularJS进入使用前的准备工作

    安装 AngularJS是以JavaScript文件形式发布的,可以通过 script 标签添加到网页中. 下载地址:https://github.com/angular/angular.js/rel ...