【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 现在小朋友们最喜欢的"喜羊羊与 ...
随机推荐
- 【Spring】使用Spring和AMQP发送接收消息(中)
上篇讲了RabbitMQ连接工厂的作用是用来创建RabbitMQ的连接,本篇就来讲讲RabbitMQ的发送消息.通过RabbitMQ发送消息最简单的方式就是将connectionFactory Bea ...
- 对JDBC的优化,BeanUtils和DBUtils
为了进一步简化jdbc的使用,就是用组件进一步的及优化 BeanUtils工具包,代替java本身蹩脚的javaBean,使对象的封装更加的简单易行 DBUtils工具包,是jdbc的操作更加的简单 ...
- [LeetCode] 01 Matrix 题解
题意 # 思路 我一开始的时候想的是嘴 # 实现 ```cpp // // include "../PreLoad.h" class Solution { public: /** ...
- 解决Chrome动画”卡顿”的办法
为动画DOM元素添加CSS3样式-webkit-transform:transition3d(0,0,0)或-webkit-transform:translateZ(0);,这两个属性都会开启GPU硬 ...
- 使用Android Studio导入第三方库项目
在使用Android Studio开发时,用到了第三方库SlidingMenu(现在已经不推荐使用了),尽管如此,但具体怎么导入第三方库还是需要知道的,在查阅各种资料后,知道了一种比较容易可行的方法 ...
- JSON与JAVA的数据转换
http://developer.51cto.com/art/200906/129090.htm java.lang.ClassNotFoundException: net.sf.json.JSONA ...
- 任务调度之持久化(基于Quartz.net)
上一篇我们了解了任务调度及他的远端管理方式,传送门:任务调度及远端管理(基于Quartz.net) 这篇我们要完成任务调度的持久化功能,即新增修改删除之类的功能,这必须得要有的,不然都不知道后台都有什 ...
- Web服务器磁盘满故障深入解析
问题:硬盘显示被写满,但是用du -sh /*查看时占用硬盘空间之和还远小于硬盘大小即找不到硬盘分区是怎么被写满的. 今天下午接到一学生紧急求助,说生产线服务器硬盘满了.该删的日志都删掉了.可空间还是 ...
- linux 下日常使用便利工具
Nautilus 你工作中有在GUI和命令行之间切来切去吗?当你总是要在命令行中输入你要进入的目录的时候,你有沮丧无奈过吗?如果有的话,那么,你一定要试下这个nautilus插件 —— nautilu ...
- C#, VB.NET如何加密PDF文档
在日常工作中,人们通常通过加密PDF文档的方式来保护PDF文档.不管是公司还是个人,使用PDF加密术来设置一些权限是必不可少的.为了使PDF文档既可读又不能被未授权的用户所更改,一份PDF文档往往需要 ...