【LOJ3156】「NOI2019」回家路线

【题目链接】

【思路要点】

  • 按照每条边的 q i q_i qi 排序,则显然有 O ( M 2 ) O(M^2) O(M2) 的动态规划做法。
  • 将每一条边看做两个事件,一个在 p i p_i pi 时刻的询问 d p dp dp 值的事件和一个在 q i q_i qi 时刻的插入 d p dp dp 值的事件,则我们只需要快速进行转移。
  • 注意到代价函数时关于时间距离的二次函数,且各项系数均非负,转移显然满足决策单调性,直接用决策单调性优化即可。
  • 时间复杂度 O ( M L o g M ) O(MLogM) O(MLogM)

【代码】

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 2e5 + 5;
const int MAXV = 1e3;
const long long INF = 4e9;
template <typename T> void chkmax(T &x, T y) {x = max(x, y); }
template <typename T> void chkmin(T &x, T y) {x = min(x, y); }
template <typename T> void read(T &x) {
	x = 0; int f = 1;
	char ch = getchar();
	for (; !isdigit(ch); ch = getchar()) if (ch == '-') f = -f;
	for (; isdigit(ch); ch = getchar()) x = x * 10 + ch - '0';
	x *= f;
}
ll A, B, C;
ll func(int x) {
	return A * x * x + B * x + C;
}
struct info {int x, y, s, t; } a[MAXN];
struct pinfo {int home, l, r; } q[MAXN];
struct event {bool type; int when, home; } b[MAXN * 2];
ll dp[MAXN]; int n, m, tot;
int cnt[MAXN], ql[MAXN], qr[MAXN];
bool cmp(info a, info b) {return a.t < b.t; }
bool cnp(event a, event b) {
	if (a.when == b.when) return a.type > b.type;
	else return a.when < b.when;
}
ll trans(int from, int when) {
	return dp[from] + func(when - a[from].t);
}
int main() {
	freopen("route.in", "r", stdin);
	freopen("route.out", "w", stdout);
	read(n), read(m), read(A), read(B), B++, read(C);
	cnt[1]++, dp[0] = 0;
	for (int i = 1; i <= m; i++) {
		read(a[i].x), read(a[i].y);
		read(a[i].s), read(a[i].t);
		cnt[a[i].y]++;
	}
	sort(a + 1, a + m + 1, cmp);
	for (int i = 1; i <= n; i++) {
		ql[i] = cnt[i - 1] + 1;
		qr[i] = cnt[i - 1];
		cnt[i] += cnt[i - 1];
	}
	for (int i = 1; i <= m; i++) {
		b[++tot] = (event) {false, a[i].s, i};
		b[++tot] = (event) {true, a[i].t, i};
	}
	sort(b + 1, b + tot + 1, cnp);
	q[++qr[1]] = (pinfo) {0, 0, MAXV};
	for (int i = 1; i <= tot; i++) {
		int timer = b[i].when, home = b[i].home;
		if (!b[i].type) {
			int pos = a[home].x;
			while (ql[pos] <= qr[pos] && timer > q[ql[pos]].r) ql[pos]++;
			if (ql[pos] <= qr[pos]) {
				chkmax(q[ql[pos]].l, timer);
				dp[home] = trans(q[ql[pos]].home, timer) + a[home].t - a[home].s;
			} else dp[home] = INF;
		} else {
			int pos = a[home].y;
			while (ql[pos] <= qr[pos] && timer > q[ql[pos]].r) ql[pos]++;
			if (ql[pos] <= qr[pos]) chkmax(q[ql[pos]].l, timer);
			while (ql[pos] <= qr[pos] && trans(home, q[qr[pos]].l) <= trans(q[qr[pos]].home, q[qr[pos]].l)) qr[pos]--;
			if (ql[pos] > qr[pos]) q[++qr[pos]] = (pinfo) {home, timer, MAXV};
			else if (trans(home, MAXV) >= trans(q[qr[pos]].home, MAXV)) q[qr[pos]].r = MAXV; 
			else {
				int l = q[qr[pos]].l, r = MAXV;
				while (l < r) {
					int mid = (l + r) / 2;
					if (trans(home, mid) <= trans(q[qr[pos]].home, mid)) r = mid;
					else l = mid + 1;
				}
				q[qr[pos]].r = l - 1;
				q[++qr[pos]] = (pinfo) {home, l, MAXV};
			}
		}
	}
	ll ans = INF;
	for (int i = 1; i <= m; i++)
		if (a[i].y == n) chkmin(ans, dp[i]);
	cout << ans << endl;
	return 0;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值