洛谷 P3227 [HNOI2013]切糕(最小割)
题解
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]切糕(最小割)的更多相关文章
- [洛谷P3227][HNOI2013]切糕
题目大意:有一个$n\times m$的切糕,每一个位置的高度可以在$[1,k]$之间,每个高度有一个代价,要求四联通的两个格子之间高度最多相差$D$,问可行的最小代价.$n,m,k,D\leqsla ...
- Luogu P3227 [HNOI2013]切糕 最小割
首先推荐一个写的很好的题解,个人水平有限只能写流水账,还请见谅. 经典的最小割模型,很多人都说这个题是水题,但我还是被卡了=_= 技巧:加边表示限制 在没有距离\(<=d\)的限制时候,我们对每 ...
- 洛谷$P3227\ [HNOI2013]$切糕 网络流
正解:网络流 解题报告: 传送门! 日常看不懂题系列,,,$QAQ$ 所以先放下题目大意趴$QwQ$,就说有个$p\cdot q$的矩阵,每个位置可以填一个$[1,R]$范围内的整数$a_{i,j}$ ...
- bzoj3144 [HNOI2013]切糕(最小割)
bzoj3144 [HNOI2013]切糕(最小割) bzoj Luogu 题面描述见上 题解时间 一开始我真就把这玩意所说的切面当成了平面来做的 事实上只是说相邻的切点高度差都不超过 $ d $ 对 ...
- bzoj 3144: [Hnoi2013]切糕 最小割
3144: [Hnoi2013]切糕 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 681 Solved: 375[Submit][Status] ...
- 【BZOJ3144】[Hnoi2013]切糕 最小割
[BZOJ3144][Hnoi2013]切糕 Description Input 第一行是三个正整数P,Q,R,表示切糕的长P. 宽Q.高R.第二行有一个非负整数D,表示光滑性要求.接下来是R个P行Q ...
- 【洛谷P3329】 [ZJOI2011]最小割(最小割树)
洛谷 题意: 给出一个无向图,之后有\(q,q\leq 30\)组询问,每组询问有一个\(x\),回答有多少点对\((a,b)\)其\(a-b\)最小割不超过\(x\). 思路: 这个题做法要最小割树 ...
- BZOJ3144[Hnoi2013]切糕——最小割
题目描述 输入 第一行是三个正整数P,Q,R,表示切糕的长P. 宽Q.高R.第二行有一个非负整数D,表示光滑性要求.接下来是R个P行Q列的矩阵,第z个 矩阵的第x行第y列是v(x,y,z) (1≤x≤ ...
- bzoj 3144 [Hnoi2013]切糕——最小割
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3144 一根纵轴上切一个点,可以把一根纵轴上的点连成一串来体现.自己的写法是每个点连向前一个点 ...
随机推荐
- 使用python把图片存入数据库-乾颐堂
一般情况下我们是把图片存储在文件系统中,而只在数据库中存储文件路径的,但是有时候也会有特殊的需求:把图片二进制存入数据库. 今天我们采用的是python+mysql的方式 MYSQL 是支持把图片存入 ...
- duilib界面库
xml编写界面库 notify控制程序 win32程序 winmain主函数 复杂控件自绘
- 【原创】cython and python for kenlm
未经允许不可转载 Kenlm相关知识 Kenlm下载地址 kenlm中文版本训练语言模型 如何使用kenlm训练出来的模型C++版本 关于Kenlm模块的使用及C++源码说明 加载Kenlm模块命令 ...
- Oracle 用户验证日志
1.sysdba/sysoper 权限用户验证日志;2.非sysdba/sysoper 权限用户验证日志;3.关于sqlcode; 1.sysdba/sysoper 权限用户验证日志:在数据库设置了参 ...
- CENTOS7 YUM安装BOOST1.53(静态版本)
按照之前的博文更新163的源之后,执行: yum install boost-static.i686 yum install boost-devel.i686 yum install boost-do ...
- DapperExtensions 使用教程
最近搭建一个框架,使用dapper来做数据库访问,数据是sql server2012,支持多个数据库.事务.orm.ado.net原生操作方式,非常方便. 使用dapper的原因网上有很多文章说明,这 ...
- MongoDB与CouchDB全方位对比(转)
出处:http://www.csdn.net/article/2011-03-21/294226 本文见于MongoDB官方网站,MongoDB与CouchDB很相似,他们都是文档型存储,数据存储格式 ...
- CentOS 7.2配置Apache服务httpd(上)
http://www.jb51.net/article/97434.htm 二.安装Apache httpd 安装httpd以配置Web服务器, HTTP使用80 / TCP ? 1 2 3 4 5 ...
- 【Cocos2d-x】Cocos2d-x跨Android平台搭建之四:Win7 64位+ eclipse + cocos2dX
开始研究cocos2dx,mark一下这个的配置步骤 1 下载eclipse 2 下载android sdk,配置sdk路径,添加环境变量 3 安装adt 4 下载android ndk,配 ...
- <a>标签的用法以及[@text_cut]
<a href="${a.url}" target="_blank">[@text_cut s=a.title len=titLen append= ...