题解

Dinic求最小割

题目其实就是求最小的代价使得每个纵轴被分成两部分

最小割!!!

我们把每个点抽象成一条边,一个纵轴就是一条\(S-T\)的路径

但是题目要求\(|f(x,y)-f(x’,y’)| ≤D\)

不能直接跑最小割

考虑如何限制

首先,\(|f(x,y)-f(x’,y’)| ≤D\)是相互的

所以只要考虑 \(f(x,y)-f(x',y')\leq D\)

限制想一想看代码就明白了

代码就很简洁了

Code

#include<bits/stdc++.h>

#define LL long long
#define RG register using namespace std;
template<class T> inline void read(T &x) {
x = 0; RG char c = getchar(); bool f = 0;
while (c != '-' && (c < '0' || c > '9')) c = getchar(); if (c == '-') c = getchar(), f = 1;
while (c >= '0' && c <= '9') x = x*10+c-48, c = getchar();
x = f ? -x : x;
return ;
}
template<class T> inline void write(T x) {
if (!x) {putchar(48);return ;}
if (x < 0) x = -x, putchar('-');
int len = -1, z[20]; while (x > 0) z[++len] = x%10, x /= 10;
for (RG int i = len; i >= 0; i--) putchar(z[i]+48);return ;
} const int N = 80000, inf = 2147483647; struct node {
int to, nxt, w;
}g[2000000];
int last[N], gl = 1; void add(int x, int y, int z) {
g[++gl] = (node) {y, last[x], z};
last[x] = gl;
g[++gl] = (node) {x, last[y], 0};
last[y] = gl;
} queue<int> q;
int dep[N], s, t, cur[N]; bool bfs() {
memset(dep, 0, sizeof(dep));
dep[s] = 1;
q.push(s);
while (!q.empty()) {
int u = q.front(); q.pop();
for (int i = last[u]; i; i = g[i].nxt) {
int v = g[i].to;
if (!dep[v] && g[i].w) {
dep[v] = dep[u]+1;
q.push(v);
}
}
}
return dep[t] == 0 ? 0 : 1;
} int dfs(int u, int d) {
if (u == t) return d;
for (int &i = cur[u]; i; i = g[i].nxt) {
int v = g[i].to;
if (dep[v] == dep[u]+1 && g[i].w) {
int di = dfs(v, min(d, g[i].w));
if (di) {
g[i].w -= di;
g[i^1].w += di;
return di;
}
}
}
return 0;
} int Dinic() {
int ans = 0;
while (bfs()) {
for (int i = 1; i <= t; i++) cur[i] = last[i];
while (int d = dfs(s, inf)) ans += d;
}
return ans;
} int a[50][50][50], id[50][50][50]; int fx[] = {0, 1, -1, 0};
int fy[] = {1, 0, 0, -1}; int main() {
int p, q, r, d, tot = 0;
read(p), read(q), read(r), read(d);
for (int i = 1; i <= r; i++)
for (int j = 1; j <= p; j++)
for (int k = 1; k <= q; k++)
read(a[i][j][k]), id[i][j][k] = ++tot;
for (int j = 1; j <= p; j++)
for (int k = 1; k <= q; k++)
id[r+1][j][k] = ++tot;
s = tot+1, t = s+1;
for (int i = 1; i <= p; i++)
for (int j = 1; j <= q; j++)
add(s, id[1][i][j], inf), add(id[r+1][i][j], t, inf); for (int k = 1; k <= r; k++)
for (int i = 1; i <= p; i++)
for (int j = 1; j <= q; j++)
add(id[k][i][j], id[k+1][i][j], a[k][i][j]); for (int k = d+1; k <= r+1; k++)
for (int i = 1; i <= p; i++)
for (int j = 1; j <= q; j++) {
for (int z = 0; z < 4; z++) {
int x = i + fx[z], y = j + fy[z];
if (x < 1 || y < 1 || x > p || y > q) continue;
add(id[k][i][j], id[k-d][x][y], inf);
}
} printf("%d\n", Dinic());
return 0;
}

洛谷 P3227 [HNOI2013]切糕(最小割)的更多相关文章

  1. [洛谷P3227][HNOI2013]切糕

    题目大意:有一个$n\times m$的切糕,每一个位置的高度可以在$[1,k]$之间,每个高度有一个代价,要求四联通的两个格子之间高度最多相差$D$,问可行的最小代价.$n,m,k,D\leqsla ...

  2. Luogu P3227 [HNOI2013]切糕 最小割

    首先推荐一个写的很好的题解,个人水平有限只能写流水账,还请见谅. 经典的最小割模型,很多人都说这个题是水题,但我还是被卡了=_= 技巧:加边表示限制 在没有距离\(<=d\)的限制时候,我们对每 ...

  3. 洛谷$P3227\ [HNOI2013]$切糕 网络流

    正解:网络流 解题报告: 传送门! 日常看不懂题系列,,,$QAQ$ 所以先放下题目大意趴$QwQ$,就说有个$p\cdot q$的矩阵,每个位置可以填一个$[1,R]$范围内的整数$a_{i,j}$ ...

  4. bzoj3144 [HNOI2013]切糕(最小割)

    bzoj3144 [HNOI2013]切糕(最小割) bzoj Luogu 题面描述见上 题解时间 一开始我真就把这玩意所说的切面当成了平面来做的 事实上只是说相邻的切点高度差都不超过 $ d $ 对 ...

  5. bzoj 3144: [Hnoi2013]切糕 最小割

    3144: [Hnoi2013]切糕 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 681  Solved: 375[Submit][Status] ...

  6. 【BZOJ3144】[Hnoi2013]切糕 最小割

    [BZOJ3144][Hnoi2013]切糕 Description Input 第一行是三个正整数P,Q,R,表示切糕的长P. 宽Q.高R.第二行有一个非负整数D,表示光滑性要求.接下来是R个P行Q ...

  7. 【洛谷P3329】 [ZJOI2011]最小割(最小割树)

    洛谷 题意: 给出一个无向图,之后有\(q,q\leq 30\)组询问,每组询问有一个\(x\),回答有多少点对\((a,b)\)其\(a-b\)最小割不超过\(x\). 思路: 这个题做法要最小割树 ...

  8. BZOJ3144[Hnoi2013]切糕——最小割

    题目描述 输入 第一行是三个正整数P,Q,R,表示切糕的长P. 宽Q.高R.第二行有一个非负整数D,表示光滑性要求.接下来是R个P行Q列的矩阵,第z个 矩阵的第x行第y列是v(x,y,z) (1≤x≤ ...

  9. bzoj 3144 [Hnoi2013]切糕——最小割

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3144 一根纵轴上切一个点,可以把一根纵轴上的点连成一串来体现.自己的写法是每个点连向前一个点 ...

随机推荐

  1. g2o 图优化

    http://www.cnblogs.com/gaoxiang12/p/5244828.html 扩展里面csparse

  2. VMware 桥接模式 复制物理网络连接状态的作用

    参考: https://docs.vmware.com/cn/VMware-Workstation-Pro/15.0/com.vmware.ws.using.doc/GUID-826323AD-D01 ...

  3. Reading——简约至上

    读书感言: 简约至上——Giles Colborne,我去,这是哪里来的渣书,通篇都是泛泛而谈,实在受不鸟了> <,没学到啥实质性的东西,论述一大堆.!!!还姐的20多块钱.最讨厌这样的书 ...

  4. 并查集 - 1611 The Suspects

    题目地址: http://poj.org/problem?id=1611 分析: - 数据结构 - parent[x] 表示 x 元素的父节点位置. - rank[x] 记录x的子链的长度, 以便在合 ...

  5. C/C++语言中指针数组和数组指针比较区别

    指针数组:int *p[3] 定义一个指针数组,其中每个数组元素指向一个int型变量的地址 意思就是指针的数组,数组里面都是指针 例子: int *p[3];//定义了一个指针数组,有3个成员,每个成 ...

  6. ThinkPhp数据缓存技术

    1.缓存初始化 在 ThinkPHP 中,有一个专门处理缓存的类:Cache.class.php(在Thinkphp/Library/Think/cache.class.php,其他的各种缓存类也在这 ...

  7. C++学习--入口函数

    在学习第一个C++程序的时候发现控制台程序的入口函数是int _tmain而不是main,查了资料才发现_tmain()是为了支持unicode所使用的main一个别名,宏定义在<stdafx. ...

  8. 《深入理解Elasticsearch》README

    书目 <深入理解ElasticSearch>拉斐尔·酷奇,马雷克·罗戈任斯基[著]张世武,余洪森,商旦[译] 机械工业出版社,2016.1 本系列包括以下8篇笔记 第01章 Elastic ...

  9. ERROR: from PIL import Image ImportError: No module named PIL

    ERROR: from PIL import Image ImportError: No module named PIL 到 http://www.pythonware.com/products/p ...

  10. centos安装mysql,tomcat

    软件下载: jre和jdk下载:http://www.oracle.com/technetwork/java/javase/downloads/java-archive-downloads-javas ...