2016北京集训测试赛(六)Problem B: 矩阵

Solution
最小割.
参考BZOJ 3144切糕
在那道题的基础上将建图方法稍作变形: 我们对格子进行黑白染色, 对于两个格子之和\(\le k\)的限制, 就可以确定其中一个是白色格子, 一个是黑色格子. 我们让黑色格子和白色格子的点的顺序相反, 就可以表示限制了.
目前的代码还是WA的.
#include <cstdio>
#include <cctype>
#include <vector>
#include <deque>
#include <algorithm>
namespace Zeonfai
{
inline int getInt()
{
int a = 0, sgn = 1;
char c;
while(! isdigit(c = getchar())) if(c == '-') sgn *= -1;
while(isdigit(c)) a = a * 10 + c - '0', c = getchar();
return a * sgn;
}
}
const int N = 50, M = 50, INF = (int)1e9;
int D[N][M], R[N][M];
struct graph
{
struct node;
struct edge
{
node *v; int cap; edge *op;
};
struct node
{
std::vector<edge*> edg;
int dep;
inline node()
{
edg.clear();
}
}nd[N][M][11], *S, *T;
inline graph()
{
S = new node; T = new node;
}
inline void addEdge(node *u, node *v, int cap)
{
edge *a = new edge, *b = new edge;
a->v = v; a->cap = cap; a->op = b;
b->v = u; b->cap = 0; b->op = a;
u->edg.push_back(a); v->edg.push_back(b);
}
int cnt;
void clear(node *u)
{
u->dep = - cnt;
for(auto edg : u->edg) if(edg->v->dep != -cnt) clear(edg->v);
}
inline int BFS()
{
++ cnt; clear(S); S->dep = 0;
static std::deque<node*> que; que.clear();
que.push_back(S);
for(; ! que.empty(); que.pop_front())
{
node *u = que.front();
for(auto edg : u->edg) if(edg->cap && edg->v->dep == - cnt) edg->v->dep = u->dep + 1, que.push_back(edg->v);
}
return T->dep != - cnt;
}
int DFS(node *u, int flw)
{
if(u == T) return flw;
int flowSum = 0;
for(auto edg : u->edg) if(edg->cap && edg->v->dep == u->dep + 1)
{
int currentFlow = DFS(edg->v, std::min(edg->cap, flw - flowSum));
flowSum += currentFlow;
edg->cap -= currentFlow; edg->op->cap += currentFlow;
if(flowSum == flw) return flowSum;
}
if(! flowSum) u->dep = -1;
return flowSum;
}
inline int dinic()
{
cnt = 0;
int res = 0;
while(BFS())
res += DFS(S, INF);
return res;
}
}G;
int main()
{
#ifndef ONLINE_JUDGE
freopen("matrix.in", "r", stdin);
freopen("matrix.out", "w", stdout);
#endif
using namespace Zeonfai;
int n = getInt(), m = getInt();
for(int i = 0; i < n - 1; ++ i) for(int j = 0; j < m; ++ j) D[i][j] = getInt();
for(int i = 0; i < n; ++ i) for(int j = 0; j < m - 1; ++ j) R[i][j] = getInt();
for(int i = 0; i < n; ++ i) for(int j = 0; j < m; ++ j)
{
if(i ^ j & 1)
{
G.addEdge(G.S, &G.nd[i][j][9], INF);
for(int k = 9; k >= 1; -- k) G.addEdge(&G.nd[i][j][k], &G.nd[i][j][k - 1], 10 - k);
G.addEdge(&G.nd[i][j][0], G.T, INF);
for(int k = 0; k <= 9; ++ k)
{
if(i + 1 < n && D[i][j] - k <= 10 && D[i][j] - k >= 1) G.addEdge(&G.nd[i + 1][j][D[i][j] - k], &G.nd[i][j][k], INF);
if(j + 1 < m && R[i][j] - k <= 10 && R[i][j] - k >= 1) G.addEdge(&G.nd[i][j + 1][R[i][j] - k], &G.nd[i][j][k], INF);
}
}
else
{
G.addEdge(G.S, &G.nd[i][j][1], INF);
for(int k = 1; k <= 9; ++ k) G.addEdge(&G.nd[i][j][k], &G.nd[i][j][k + 1], 10 - k);
G.addEdge(&G.nd[i][j][10], G.T, INF);
for(int k = 1; k <= 10; ++ k)
{
if(i + 1 < n && D[i][j] - k <= 9 && D[i][j] - k >= 0) G.addEdge(&G.nd[i][j][k], &G.nd[i + 1][j][D[i][j] - k], INF);
if(j + 1 < m && R[i][j] - k <= 9 && R[i][j] - k >= 0) G.addEdge(&G.nd[i][j][k], &G.nd[i][j + 1][R[i][j] - k], INF);
}
}
}
printf("%d\n", n * m * 10 - G.dinic());
}
2016北京集训测试赛(六)Problem B: 矩阵的更多相关文章
- 2016北京集训测试赛(十六)Problem C: ball
Solution 这是一道好题. 考虑球体的体积是怎么计算的: 我们令\(f_k(r)\)表示\(x\)维单位球的体积, 则 \[ f_k(1) = \int_{-1}^1 f_{k - 1}(\sq ...
- 2016北京集训测试赛(十六)Problem B: river
Solution 这题实际上并不是构造题, 而是一道网络流. 我们考虑题目要求的一条路径应该是什么样子的: 它是一个环, 并且满足每个点有且仅有一条出边, 一条入边, 同时这两条边的权值还必须不一样. ...
- 2016北京集训测试赛(十六)Problem A: 任务安排
Solution 这道题告诉我们, 不能看着数据范围来推测正解的时间复杂度. 事实证明, 只要常数足够小, \(5 \times 10^6\)也是可以跑\(O(n \log n)\)算法的!!! 这道 ...
- 2016北京集训测试赛(六)Problem A: 冒泡排序
Solution 观察冒泡排序的过程. 我们注意到, 每一轮的排序都会使得每个数后面比它小的数的个数减\(1\). 我们用\(f(n, m)\)表示对\(1\)到\(n\)的一个排列进行冒泡排序, 满 ...
- 【2016北京集训测试赛(十六)】 River (最大流)
Description Special Judge Hint 注意是全程不能经过两个相同的景点,并且一天的开始和结束不能用同样的交通方式. 题解 题目大意:给定两组点,每组有$n$个点,有若干条跨组 ...
- 2016北京集训测试赛(十七)Problem C: 数组
Solution 线段树好题. 我们考虑用last[i]表示\(i\)这个位置的颜色的上一个出现位置. 考虑以一个位置\(R\)为右端点的区间最远能向左延伸到什么位置: \(L = \max_{i \ ...
- 2016北京集训测试赛(十七)Problem B: 银河战舰
Solution 好题, 又是长链剖分2333 考虑怎么统计答案, 我场上的思路是统计以一个点作为结尾的最长上升链, 但这显然是很难处理的. 正解的方法是统计以每个点作为折弯点的最长上升链. 具体的内 ...
- 2016北京集训测试赛(十七)Problem A: crash的游戏
Solution 相当于要你计算这样一个式子: \[ \sum_{x = 0}^m \left( \begin{array}{} m \\ x \end{array} \right) \left( \ ...
- BZOJ 4543 2016北京集训测试赛(二)Problem B: thr 既 长链剖分学习笔记
Solution 这题的解法很妙啊... 考虑这三个点可能的形态: 令它们的重心为距离到这三个点都相同的节点, 则其中两个点分别在重心的两棵子树中, 且到重心的距离相等; 第三个点可能在重心的一棵不同 ...
随机推荐
- plsql 编程基础
分支 declare --声明变量 a ); b ); c ); begin --开始 a := '小明'; dbms_output.put_line(a); b :; c :; if b > ...
- loadrunner11使用wplus_init_wsock录制非IE脚本/手机抓包
假如你的IE版本号太高,或者是chrome,firfox,又或者想录手机上的包,可以用loadrunner的wplus_init_wsock工具 1.在创建LR脚本的Start Recording ...
- Leetcode 630.课程表III
课程表III 这里有 n 门不同的在线课程,他们按从 1 到 n 编号.每一门课程有一定的持续上课时间(课程时间)t 以及关闭时间第 d 天.一门课要持续学习 t 天直到第 d天时要完成,你将会从第 ...
- [转]核函数K(kernel function)
1 核函数K(kernel function)定义 核函数K(kernel function)就是指K(x, y) = <f(x), f(y)>,其中x和y是n维的输入值,f(·) 是从n ...
- 利用python爬取58同城简历数据
利用python爬取58同城简历数据 利用python爬取58同城简历数据 最近接到一个工作,需要获取58同城上面的简历信息(http://gz.58.com/qzyewu/).最开始想到是用pyth ...
- docker镜像与docker容器的区别
镜像的一个实例称为容器. 你有一个镜像,这是你描述的一组图层. 如果你开始这个镜像,你有一个运行这个镜像的容器. 您可以拥有许多相同镜像的正在运行的容器. docker images 查看所有镜像 d ...
- hadoop配置文件: hdfs-site.xml, mapred-site.xml
dfs.name.dir Determines where on the local filesystem the DFS name node should store the name table( ...
- php获取客户端mac地址
exec('/sbin/arp -a 2>&1', $array, $return_val);dump($array);$mac = '';foreach($array as $valu ...
- 快乐的Linux命令行
ls - 列出目录内容 -a 列出所有文件 -d 指定目录信息 -F 为目录增加/标识 -h 增强可读性 -l 列模式显示 -r 反序显示 -S 按照大小排序 -t 按照修改时间排序 file - 确 ...
- BZOJ 1083:[SCOI2005]繁忙的都市(最小生成树)
1083: [SCOI2005]繁忙的都市 Description 城市C是一个非常繁忙的大都市,城市中的道路十分的拥挤,于是市长决定对其中的道路进行改造.城市C的道路是这样分布的:城市中有n个交叉路 ...