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

题解:网络流,不考虑相差为$D$的条件时,可以给每个位置建一个点,源点连向高度为$1$的点容量为$\infty$,高度为$i$的点连向这个位置高度为$i+1$的点,容量为代价,高度为$k$的连向汇点,容量为代价。跑最小割。

考虑相差为$D$的条件,可以对于相邻的两个点$A,B$,连接$A_i\to B_{i-D}$的容量为$\infty$的边这样就强制限定两个点的最小割必须相距小于等于$D$,跑最小割即可

卡点:加边的时候多加了$k$倍,导致又$TLE$又$RE$

C++ Code:

#include <cstdio>
#include <cctype>
#include <algorithm>
const int inf = 0x3f3f3f3f; namespace std {
struct istream {
#define M (1 << 24 | 3)
char buf[M], *ch = buf - 1;
inline istream() { fread(buf, 1, M, stdin); }
inline istream& operator >> (int &x) {
while (isspace(*++ch));
for (x = *ch & 15; isdigit(*++ch); ) x = x * 10 + (*ch & 15);
return *this;
}
#undef M
} cin;
struct ostream {
#define M (1 << 10 | 3)
char buf[M], *ch = buf - 1;
inline ostream& operator << (int x) {
if (!x) {*++ch = '0'; return *this;}
static int S[20], *top; top = S;
while (x) {*++top = x % 10 ^ 48; x /= 10;}
for (; top != S; --top) *++ch = *top;
return *this;
}
inline ostream& operator << (const char x) {*++ch = x; return *this;}
inline ~ostream() { fwrite(buf, 1, ch - buf + 1, stdout); }
#undef M
} cout;
} namespace Network_Flow {
const int maxn = 50 * 50 * 50, maxm = maxn * 6; int lst[maxn], head[maxn], cnt = 1;
struct Edge {
int to, nxt, w;
} e[maxm << 1];
void addedge(int a, int b, int c) {
e[++cnt] = (Edge) { b, head[a], c }; head[a] = cnt;
e[++cnt] = (Edge) { a, head[b], 0 }; head[b] = cnt;
} int st, ed, n, MF;
int GAP[maxn], dis[maxn], q[maxn], h, t;
void init() {
GAP[dis[q[h = t = 0] = ed] = 1] = 1;
for (int i = st; i <= ed; ++i) lst[i] = head[i];
while (h <= t) {
int u = q[h++];
for (int i = head[u], v; i; i = e[i].nxt) {
v = e[i].to;
if (!dis[v]) {
++GAP[dis[v] = dis[u] + 1];
q[++t] = v;
}
}
}
}
int dfs(int u, int low) {
if (!low || u == ed) return low;
int w, res = 0;
for (int &i = lst[u], v; i; i = e[i].nxt) if (e[i].w) {
v = e[i].to;
if (dis[u] == dis[v] + 1) {
w = dfs(v, std::min(low, e[i].w));
res += w, low -= w;
e[i].w -= w, e[i ^ 1].w += w;
if (!low) return res;
}
}
if (!--GAP[dis[u]]) dis[st] = n + 1;
++GAP[++dis[u]], lst[u] = head[u];
return res;
}
void ISAP(int S, int T) {
st = S, ed = T, n = T - S + 1;
init(); while (dis[st] <= n) MF += dfs(st, inf);
}
} int n, m, h, D;
int v[50][50][50];
int pos[50][50][50], idx, st, ed; void addedge(int x, int y, int z, int w) {
for (int i = D + 1; i <= h; ++i)
Network_Flow::addedge(pos[x][y][i], pos[z][w][i - D], inf);
}
int main() {
std::cin >> n >> m >> h >> D;
for (int i = 1; i <= h; ++i)
for (int j = 1; j <= n; ++j)
for (int k = 1; k <= m; ++k) {
pos[j][k][i] = ++idx;
std::cin >> v[j][k][i];
}
st = 0, ed = ++idx;
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= m; ++j) {
for (int k = 1; k <= h; ++k) {
if (k == 1) Network_Flow::addedge(st, pos[i][j][k], inf);
if (k == h) Network_Flow::addedge(pos[i][j][k], ed, v[i][j][k]);
else Network_Flow::addedge(pos[i][j][k], pos[i][j][k + 1], v[i][j][k]);
}
if (i != 1) addedge(i, j, i - 1, j);
if (i != n) addedge(i, j, i + 1, j);
if (j != 1) addedge(i, j, i, j - 1);
if (j != n) addedge(i, j, i, j + 1);
}
Network_Flow::ISAP(st, ed);
std::cout << Network_Flow::MF << '\n';
return 0;
}

  

最新文章

  1. 测试MailUtils,作用是发邮件
  2. bootstrap笔记
  3. pb将datawindow数据导出EXCEL
  4. caffe-window搭建自己的小项目例子
  5. 简单实现web单点登录
  6. 视网膜New iPad与普通分辨率iPad页面的兼容处理
  7. 用JAVA 查询 Active Directory(AD)
  8. 使用正则表达式限制swing (JTextField等) 的输入
  9. 【IPC第二个进程间通信】管道Pipe
  10. Lock使用实例
  11. meta标签属性总结
  12. spring默认欢迎页设置
  13. 【1414软工助教】团队作业4——第一次项目冲刺(Alpha版本) 得分榜
  14. python只re模块
  15. .NET平台下,初步认识AutoMapper
  16. mac系统vscode环境配置,以及iTerm2配置Zsh + on-my-zsh shell
  17. HTTP请求报文解剖
  18. h5-语义化标签
  19. 史上最全脉搏心率传感器PulseSensor资料(电路图+中文说明书+最全源代码)
  20. ES6 let和const 的相同点与区别

热门文章

  1. mysql find_in_set 函数 使用方法
  2. PHP 之根据两个经纬度计算距离
  3. element ui分页器的使用
  4. 第10组 Beta冲刺(5/5)
  5. vue中使用百度地图vue-baidu-map
  6. 【spring源码分析】IOC容器解析
  7. rabbitmq - 消息接收,解析xml格式数据时异常:ERROR not well-formed (invalid token): line 4, column 46
  8. openresty开发系列32--openresty执行流程之1初始化阶段
  9. ubuntu 18.04屏幕共享 -------(转载) ( Windows远程登录Ubuntu )
  10. Robotics Education and Research at Scale - A Remotely Accessible Robotics Development Platform