# LOJ 3156: 「NOI2019」回家路线

### 题解：

\begin{aligned}\mathrm{f}[i]&=\min_{y_j=x_i,q_j\le p_i}\{\mathrm{f}[j]+\mathrm{A}(p_i-q_j)^2+\mathrm{B}(p_i-q_j)+\mathrm{C}\}\\&=\min_{y_j=x_i,q_j\le p_i}\{\mathrm{f}[j]+\mathrm{A}p_i^2-2\mathrm{A}p_iq_j+\mathrm{A}q_j^2+\mathrm{B}p_i-\mathrm{B}q_j+\mathrm{C}\}\\&=\min_{y_j=x_i,q_j\le p_i}\{\mathrm{f}[j]+\mathrm{A}q_j^2-\mathrm{B}q_j-2\mathrm{A}p_iq_j\}+\mathrm{A}p_i^2+\mathrm{B}p_i+\mathrm{C}\end{aligned}

$\displaystyle\mathrm{f}[i]=\mathrm{D}_i+\min_{y_j=x_i,q_j\le p_i}\{y_j-2\mathrm{A}p_ix_j\}$

\begin{aligned}y_j-2\mathrm{A}p_ix_j&<y_k-2\mathrm{A}p_ix_k\\2\mathrm{A}p_i(x_k-x_j)&<y_k-y_j\\\frac{y_k-y_j}{x_k-x_j}&>2\mathrm{A}p_i\end{aligned}

#include <cstdio>
#include <algorithm>
#include <vector>

typedef double db;
const int MN = 100005, MM = 200005, MQ = 1005;

int N, M, _A, _B, _C, Ans = 0x3f3f3f3f;
int d[MN], eu[MM], ev[MM], ep[MM], eq[MM];
std::vector<int> vp[MQ], vq[MQ];
int X[MM], Y[MM], f[MM];
int _stk[MM], *stk[MN], _l[MN], _r[MN];

inline db Slope(int i, int j) {
if (X[i] == X[j]) return Y[i] == Y[j] ? 0 : Y[i] < Y[j] ? 1e99 : -1e99;
return (db)(Y[j] - Y[i]) / (X[j] - X[i]);
}

int main() {
freopen("route.in", "r", stdin);
freopen("route.out", "w", stdout);
scanf("%d%d%d%d%d", &N, &M, &_A, &_B, &_C);
ev[0] = 1, vq[0].push_back(0), ++d[1];
for (int i = 1; i <= M; ++i) {
scanf("%d%d%d%d", &eu[i], &ev[i], &ep[i], &eq[i]);
vp[ep[i]].push_back(i);
vq[eq[i]].push_back(i);
++d[ev[i]];
}
stk[0] = _stk;
for (int i = 1; i <= N; ++i) stk[i] = stk[i - 1] + d[i - 1], _l[i] = 1;
for (int t = 0; t <= 1000; ++t) {
for (auto i : vq[t]) {
int u = ev[i], *st = stk[u], l = _l[u], &r = _r[u];
while (l < r && Slope(st[r - 1], st[r]) > Slope(st[r], i)) --r;
st[++r] = i;
if (u == N && Ans > f[i] + eq[i]) Ans = f[i] + eq[i];
}
for (auto i : vp[t]) {
int u = eu[i], *st = stk[u], &l = _l[u], r = _r[u];
while (l < r && Slope(st[l], st[l + 1]) < 2 * _A * t) ++l;
if (l <= r) f[i] = Y[st[l]] - 2 * _A * t * X[st[l]] + _A * t * t + _B * t + _C;
else f[i] = 0x3f3f3f3f;
X[i] = eq[i], Y[i] = f[i] + _A * eq[i] * eq[i] - _B * eq[i];
}
}
printf("%d\n", Ans);
return 0;
}
posted @ 2019-07-21 11:11  粉兔  阅读(...)  评论(... 编辑 收藏