【BZOJ1001】[BeiJing2006]狼抓兔子
挺简单一个题,最小割模板
我的感觉就是可能建图的时候会比较麻烦吧,毕竟三个方向。
#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]狼抓兔子的更多相关文章
- BZOJ1001 BeiJing2006 狼抓兔子 【网络流-最小割】*
BZOJ1001 BeiJing2006 狼抓兔子 Description 现在小朋友们最喜欢的"喜羊羊与灰太狼",话说灰太狼抓羊不到,但抓兔子还是比较在行的,而且现在的兔子还比较 ...
- [BZOJ1001][BeiJing2006]狼抓兔子(最小割转最短路|平面图转对偶图)
1001: [BeiJing2006]狼抓兔子 Time Limit: 15 Sec Memory Limit: 162 MBSubmit: 31805 Solved: 8494[Submit][ ...
- BZOJ1001: [BeiJing2006]狼抓兔子 [最小割 | 对偶图+spfa]
1001: [BeiJing2006]狼抓兔子 Time Limit: 15 Sec Memory Limit: 162 MBSubmit: 19528 Solved: 4818[Submit][ ...
- BZOJ1001: [BeiJing2006]狼抓兔子【最短路+对偶图】
题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1001 1001: [BeiJing2006]狼抓兔子 Time Limit: 15 Se ...
- bzoj1001: [BeiJing2006]狼抓兔子 -- 最小割
1001: [BeiJing2006]狼抓兔子 Time Limit: 15 Sec Memory Limit: 162 MB Description 现在小朋友们最喜欢的"喜羊羊与灰太狼 ...
- bzoj1001: [BeiJing2006]狼抓兔子(初识是你最小割)
1001: [BeiJing2006]狼抓兔子 题目:传送门 题解: 听说这题当初是大难题...可惜当年没有网络流hahahha 现在用网络流的思想就很容易解决了嘛 给什么连什么,注意是双向边,然后跑 ...
- BZOJ1001: [BeiJing2006]狼抓兔子(优化的dinic或转化对偶图求最短路)
1001: [BeiJing2006]狼抓兔子 Time Limit: 15 Sec Memory Limit: 162 MBSubmit: 30078 Solved: 7908[Submit][ ...
- [bzoj1001][BeiJing2006]狼抓兔子_网络流_最小割转对偶图
狼抓兔子 bzoj-1001 BeiJing2006 Description 现在小朋友们最喜欢的"喜羊羊与灰太狼",话说灰太狼抓羊不到,但抓兔子还是比较在行的, 而且现在的兔子还 ...
- BZOJ1001[BeiJing2006]狼抓兔子最小割網絡流
Description 现在小朋友们最喜欢的"喜羊羊与灰太狼",话说灰太狼抓羊不到,但抓兔子还是比较在行的, 而且现在的兔子还比较笨,它们只有两个窝,现在你做为狼王,面对下面这样一 ...
- Bzoj1001 [BeiJing2006]狼抓兔子
Time Limit: 15 Sec Memory Limit: 162 MBSubmit: 19759 Solved: 4883 Description 现在小朋友们最喜欢的"喜羊羊与 ...
随机推荐
- _1_html_
创:18_3_2017修:20_3_2017什么是html? 超文本标记语言 告诉浏览器内容的语义,html中包含了各种标签html页面的框架是什么? <!DOCTYPE html> #D ...
- YARN 命令总结
起因:YARN 使用capability schedule queue调度container,spark 的app卡死在YARN的队列里面无法出来,无奈请教大神时,可用[yarn applicatio ...
- java设计模式(1)
设计模式定义 设计模式原则 设计模式分类 常用设计模式 (一)设计模式定义 设计模式是针对软件设计中普遍存在的各种问题,所提出的解决方案. 换句话说,设计模式是一套被反复使用,多数人知晓的.经过分类的 ...
- strtol函数 将字符串转换为相应进制的整数
转自http://hi.baidu.com/qwpsmile/blog/item/9bc44efa4f41018a9f514637.html +----------------+| strt ...
- 视频swiper轮播
关于本次文章的内容,实际上是咪咕阅读详情页中的一个前端需求要做的效果,不过比起原需求,此次案例已经被删减掉许多部分了.音频部分舍弃,调用客户端接口舍弃,并做一些整理.最后留下的是这个精简版的案例.方便 ...
- Java Applet实现五子棋游戏
从谷歌的AlphaGo到腾讯的绝艺,从人脸识别到无人驾驶,从谷歌眼镜到VR的兴起,人工智能领域在不断的向前迈进,也在不断深入的探索.但背后错综复杂的技术和利益成本也是很多企业亟待解决的难题.对于人工智 ...
- Struts(五)之OGNL、contextMap
一.OGNL 1.1.定义 OGNL是Object-Graph Navigation Language的缩写,它是一个单独的开源项目. Struts2框架使用OGNL作为默认的表达式语言.它是一种功能 ...
- eclipse如何安装插件
eclipse安装插件以springsource-tool-suite为例 打开eclipse,找到help/About Eclipse/ 然后点击右下角图标 找到EclipsePlatform对应的 ...
- React-Native 之 项目实战(三)
前言 本文有配套视频,可以酌情观看. 文中内容因各人理解不同,可能会有所偏差,欢迎朋友们联系我. 文中所有内容仅供学习交流之用,不可用于商业用途,如因此引起的相关法律法规责任,与我无关. 如文中内容对 ...
- iOS 任务的依赖操作
-(void)dependency{ /** 假设有A.B~C三个操作,要求: 1. 3个操作都异步执行 2. 操作C依赖于操作B 3. 操作B依赖于操作A */ //创建一个队列 NSOperati ...