【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 现在小朋友们最喜欢的"喜羊羊与 ...
随机推荐
- TextView字体大小及颜色设置
TextView设置文字大小及颜色: 1.1)通过xml配置 <TextView android:layout_width="match_parent" a ...
- Repaints and Reflows 重绘和重排版
当浏览器下载完所有页面HTML标记,JavaScript,CSS,图片之后,它解析文件并创建两个内部数据 一棵DOM树 表示页面结构 Normal 0 7.8 磅 0 2 false false fa ...
- js放大镜
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- CDN,你了解多少?
大家对CDN并不陌生,工作中或多或少都有所接触,最近也有人问到,在此对CDN相关概念和流程做下总结,希望还能对其他朋友也有所帮助. 一.什么是CDN 维基百科上是这样定义的: CDN:内容分发网络(C ...
- 深入hibernate的三种状态
学过hibernate的人都可能都知道hibernate有三种状态,transient(瞬时状态),persistent(持久化状态)以及detached(离线状态),大家伙也许也知道这三者之间的区别 ...
- 每天学点Java小知识【1】
一 Java标识符和关键字 1.标识符 作用:用来标识类名.变量名.方法名.类型名.数组名.文件名的有效字符序列. 组成规则:由字母.下划线.美元符号和数字组成,且第一个字符不能是数字字符.注意:标识 ...
- html中的Flash对象
开源Flash播放器 http://www.open-open.com/ajax/Video.htm
- hibernate的反转引擎生成两个实体类的问题
在使用myeclipse中自带的hibernate 进行jsp开发时候遇到了这个问题.使用hibernate的反转引擎从数据库生成生成实体类,一个表生成了两个类,xx.java和xxId.java . ...
- MongoDB基础教程系列--第七篇 MongoDB 聚合管道
在讲解聚合管道(Aggregation Pipeline)之前,我们先介绍一下 MongoDB 的聚合功能,聚合操作主要用于对数据的批量处理,往往将记录按条件分组以后,然后再进行一系列操作,例如,求最 ...
- SqlDataReader生成动态Lambda表达式
上一扁使用动态lambda表达式来将DataTable转换成实体,比直接用反射快了不少.主要是首行转换的时候动态生成了委托. 后面的转换都是直接调用委托,省去了多次用反射带来的性能损失. 今天在对Sq ...