\(\mathcal{Description}\)

  Link.

  有 \(n\) 个位置,从左至右编号 \(1\sim n\)。在第 \(i\) 个位置放一座塔的代价为 \(c_i\),一个位置可以放任意数量的塔。给定 \(m\) 个要求,第 \(i\) 个表示 \([l_i,r_i]\) 内至少有 \(d_i\) 座塔。求最小的代价和。

  \(n\le10^3\),其余参数 \(\le10^4\)。

\(\mathcal{Solution}\)

  经历了逝量的 whk 学习,我学会了背套路。(

  原问题可以写成线规:

\[\begin{aligned}
\min~~~~&z=\sum_{i=1}^nc_i(x_i-x_{i-1})\\
\text{s.t.}~~~~&x_i\ge x_{i-1}\\
&x_{r_i}-x_{l_i-1}\ge d_i
\end{aligned},
\]

而它显然等价于:

\[\begin{aligned}
\min~~~~z'=\sum_{i=1}^n(c_i-c_{i+1})x_i+\sum_{i=1}^nI\max\{x_{i-1}-x_i,0\}+\sum_{i=1}^mI\max\{d_i-x_{r_i}+x_{l_i-1},0\}
\end{aligned}.
\]

​其中 \(I\) 是一个足够大的常数。

  我们的论文套路是,对于以下线规:

\[\min~~~~z=\sum_ub_ux_u+\sum_{\lang u,v\rang}c_{uv}\max\{0,x_v-x_u-w_{uv}\},
\]

其对应最小费用流模型:

  • \(b_u\) 表示 \(u\) 点的流出量-流入量。

    为了流量守恒,对于 \(b_u>0\) 的 \(u\),连 \(\lang S,u,b_u,0\rang\);

    对于 \(b_u<0\) 的 \(u\),连 \(\lang u,T,-b_u,0\rang\);

  • \(c_{uv}\) 表示 \(\lang u,v\rang\) 的容量,\(w_{uv}\) 表示 \(\lang u,v\rang\) 的费用,所以连边 \(\lang u,v,c_{uv},w_{uv}\rang\)。

该图的最小费用最大流的费用的相反数就是答案。

  点的系数出减入,max 外是容量,max 内是费用,减数连向被减数,最小费用相反数!


  但 whk 是落后的,我们来证明。令 \(f_{uv}\) 表示 \(\lang u,v\rang\) 的流量,考虑在上述条件下的最小费用流形式:

\[\begin{aligned}
\min~~~~&z=\sum_{\lang u,v\rang}c_{uv}f_{uv}\\
\text{s.t.}~~~~&-f_{uv}\ge-c_{uv}\\
&\sum_v f_{vu}-\sum_vf_{uv}=-b_u
\end{aligned}.
\]

令 \(p_{uv}\) 表示第一类限制的对偶,\(q_u\) 表示第二类限制的对偶,得到:

\[\begin{aligned}
\max~~~~&z=\sum_u -b_uq_u+\sum_{\lang u,v\rang}-c_{uv}p_{uv}\\
\text{s.t.}~~~~&q_v-q_u-p_{uv}\le w_{uv}
\end{aligned}.
\]

那么这个 \(p_{uv}\) 非常的自由,直接给它定成最优,所以

\[\min~~~~z=\sum_ub_uq_u+\sum_{\lang u,v\rang}c_{uv}\max\{0,q_v-q_u-w_{uv}\}
\]

的相反数就是答案。 \(\square\)

\(\mathcal{Code}\)

  总之得写势能 Dijkstra。

/*+Rainybunny+*/

#include <bits/stdc++.h>

#define rep(i, l, r) for (int i = l, rep##i = r; i <= rep##i; ++i)
#define per(i, r, l) for (int i = r, per##i = l; i >= per##i; --i) typedef std::pair<int, int> PII;
#define fi first
#define se second const int MAXN = 1e3, MAXM = 1e4, IINF = 0x3f3f3f3f;
int n, m, c[MAXN + 5], l[MAXM + 5], r[MAXM + 5], d[MAXM + 5]; namespace FlowGraph { const int MAXND = MAXN + 3, MAXEG = MAXN * 2 + MAXM;
int ecnt = 1, S, T, head[MAXND + 5], curh[MAXND + 5];
int hig[MAXND + 5], dis[MAXND + 5];
bool instk[MAXND + 5];
struct Edge { int to, flw, cst, nxt; } graph[MAXEG * 2 + 5]; inline void link(const int s, const int t, const int f, const int c) {
graph[++ecnt] = { t, f, c, head[s] }, head[s] = ecnt;
graph[++ecnt] = { s, 0, -c, head[t] }, head[t] = ecnt;
} inline bool spfa() {
static bool inq[MAXND + 5]; static std::queue<int> que;
rep (i, 0, T) dis[i] = IINF;
dis[S] = 0, que.push(S);
while (!que.empty()) {
int u = que.front(); que.pop(), inq[u] = false;
for (int i = head[u], v; i; i = graph[i].nxt) {
if (graph[i].flw && dis[u] + graph[i].cst < dis[v = graph[i].to]) {
dis[v] = dis[u] + graph[i].cst;
if (!inq[v]) inq[v] = true, que.push(v);
}
}
}
return dis[T] != IINF;
}
inline bool dijkstra() {
static std::priority_queue<PII, std::vector<PII>, std::greater<PII> > heap;
rep (i, 0, T) hig[i] += dis[i], dis[i] = IINF;
heap.push({ dis[S] = 0, S });
while (!heap.empty()) {
PII p(heap.top()); heap.pop();
if (dis[p.se] != p.fi) continue;
for (int i = head[p.se], v; i; i = graph[i].nxt) {
int d = p.fi + graph[i].cst + hig[p.se] - hig[v = graph[i].to];
if (graph[i].flw && dis[v] > d) heap.push({ dis[v] = d, v });
}
}
return dis[T] != IINF;
} inline PII augment(const int u, int iflw) {
if (u == T) return { iflw, 0 };
PII ret(0, 0); instk[u] = true;
for (int &i = curh[u], v; i; i = graph[i].nxt) {
if (graph[i].flw && !instk[v = graph[i].to]
&& dis[v] == dis[u] + hig[u] - hig[v] + graph[i].cst) {
PII tmp(augment(v, std::min(iflw, graph[i].flw)));
ret.fi += tmp.fi, ret.se += graph[i].cst * tmp.fi + tmp.se;
iflw -= tmp.fi, graph[i].flw -= tmp.fi, graph[i ^ 1].flw += tmp.fi;
if (!iflw) break;
}
}
if (ret.fi) instk[u] = false;
return ret;
} inline PII dinic() {
PII ret(0, 0);
for (spfa(); dijkstra(); ) {
rep (i, 0, T) curh[i] = head[i], instk[i] = false;
PII tmp(augment(S, IINF));
ret.fi += tmp.fi, ret.se += tmp.se;
}
return ret;
} } using namespace FlowGraph; int main() {
scanf("%d %d", &n, &m);
rep (i, 1, n) scanf("%d", &c[i]);
rep (i, 1, m) scanf("%d %d %d", &l[i], &r[i], &d[i]); S = n + 1, T = n + 2;
rep (i, 0, n) {
if (c[i] > c[i + 1]) link(S, i, c[i] - c[i + 1], 0);
else link(i, T, c[i + 1] - c[i], 0);
}
rep (i, 0, n - 1) link(i + 1, i, IINF, 0);
rep (i, 1, m) link(r[i], l[i] - 1, IINF, -d[i]); printf("%d\n", -dinic().se);
return 0;
}

最新文章

  1. JAVA字符串格式化-String.format()的使用(转)
  2. sass编译
  3. ThinkPad L440 FN键设置
  4. git svn clone时间估算
  5. .Net Core开源通讯组件 SmartRoute(服务即集群)
  6. Win10/UWP新特性系列—Launcher实现应用间的通信
  7. H5 认识canvas
  8. 深入浅出Attribute (转载)
  9. Notes on the Dirichlet Distribution and Dirichlet Process
  10. apple-touch-icon,shortcut icon和icon的区别
  11. firefox ie chrome 设置单元格宽度 td width 有bug,不能正常工作。以下方式可以解决
  12. Date Time Picker控件
  13. vc 制作图片资源dll
  14. php基础(六)Include
  15. CentOS7 安装Python
  16. 课程8:《Maven精品教程视频》--视频目录
  17. day5_不能循环删除list-深拷贝、浅拷贝(import copy)
  18. delphi with... do和自定义变量重名
  19. VS2010/MFC编程入门之十一(对话框:模态对话框及其弹出过程)
  20. JavaScript 对象属性

热门文章

  1. react中antd+css Module一起使用
  2. 使用swagger生成API文档
  3. vue3.0+vite+ts项目搭建--基础配置(二)
  4. HW防守 | Linux应急响应基础
  5. Vue 动态设置图片路径
  6. javaObject类—getClass方法
  7. 西安腾讯DevOps面试题python算法输出列表数值下界
  8. kubernetes之部署dashboard 和heapster
  9. python 如何获取当前系统的时间
  10. Photoshop如何快速扣取图标