洛谷 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 一根纵轴上切一个点,可以把一根纵轴上的点连成一串来体现.自己的写法是每个点连向前一个点 ...
随机推荐
- HttpMessageConverter和ContentNegotiatingViewResolver
HttpMessageConverter 在SpringMVC中,可以使用@RequestBody和@ResponseBody两个注解,分别完成请求报文到对象和对象到响应报文的转换,HttpMessa ...
- CentOS7 安装Maven3
下载安装文件 cd /root wget http://mirrors.tuna.tsinghua.edu.cn/apache/maven/maven-3/3.3.9/binaries/apache- ...
- HDU 2084 数塔 (水DP)
题意:.... 析:从下往上算即可,水DP. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #incl ...
- eclipse使用配置
eclipse设置工作空间为UTF-8(适应中文,避免乱码) windows---preference---general---workspace,修改textfile encoding eclips ...
- JDK8新特性:使用stream、Comparator和Method Reference实现集合的优雅排序
大家对java接口Comparator和Comparable都不陌生,JDK8里面Comparable还和以前一样,没有什么改动:但是Comparator在之前基础上增加了很多static和defau ...
- java爬虫入门
本文内容 涞源于 罗刚 老师的 书籍 << 自己动手写网络爬虫一书 >> ; 本文将介绍 1: 网络爬虫的是做什么的? 2: 手动写一个简单的网络爬虫; 1: 网络爬虫是做 ...
- Python 数据分析—第十章 日期处理
日期时间数据类型及工具 from datetime import datetime now = datetime.now() print(now.year,now.month,now.day) #以毫 ...
- oracle 非sys用户创建新用户 授权后 plsql看不到视图
问题: oracle 非sys用户创建新用户 授权后 plsql看不到视图 答案: 新用户查询视图时,视图名称前需要添加 视图所属用户. 如user用户新建newUser用户,newUser用户查 ...
- Android-什么时候用ScrollView
什么时候用 ScrollView ? 答:像以下这种情况,就是使用ScrollView: 摆放无顺序,无规律,并会超出屏幕的高度,就可以用ScrollView 错误的ScrollView示范,Scro ...
- C# xsd 验证 XML数据有效性 问题
使用XSD进行批量数据导入时生成的XML数据有效性这样的功能已经不是第一次做了,之前做的时候都没有碰到什么问题,这些天在开发中遇到了一个很头痛的问题就是无论XSD文件规则怎么写,验证都是通过的. 下面 ...