跳到正文

个人记录

动态最短路 (path)

Pigsyy约 6 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年4月6日
篇幅
约 6 分钟

版本

  1. v1当前

动态最短路 (path)

题目来源:20252025 年省选集训第二轮 Day 0。

§1\S 1\quad 题意

给定 nn 个点 mm 条边的有向无环图(保证所有边 u→vu\to v 满足 u<vu<v),其中每条边为黑色或白色。

接下来有 qq 次询问,每次给出两个正整数 a,ba,b 和点 xx:试求将所有黑边权值设为 aa,白边设为 bb,11 到 xx 的最短路长度为多少?

对于所有测试点,保证 n≤50000n\le 50000,m≤105m\le 10^5。

测试点编号n,q≤n,q\le m≤m\le 特殊性质
1∼51\sim 51000100020002000无
6∼106\sim 10500050001000010000无
11∼1511\sim 155000050000100000100000vi≤ui+2v_i\le u_i+2
16∼2516\sim 255000050000100000100000无

§2\S2\quad 思路

§2.1\S2.1\quad O(nm+nq)O(nm+nq) 做法

考虑每个点实际上只需要记录两个信息:(x,y)(x,y) —— 分别表示经过的黑边的数量 xx 和白边数量 yy。

那么,如果存储出了所有这些 (x,y)(x,y),则每次查询的时候对于每个 (x,y)(x,y) 对应的权值为 ax+byax+by,只需要从这些中取出最小值。

而这样的 (x,y)(x,y) 有多少个呢?注意到,同样的 xx 只需要记录最小的 yy,所以对于每个点只有 O(n)O(n) 个。

综上,时间复杂度为 O(nm+nq)O(nm+nq)。

§2.2\S 2.2\quad O(n5/3)O(n^{5/3}) 做法

沿用 §2.1\S 2.1 的做法,考虑答案的形式是 ans=ax+byans=ax+by。那么,这实际上对应着 ax+by−ansax+by-ans 这条直线,而希望截距尽可能小,不难想到最优必然在下凸壳上取到。

经典结论:若干个横纵坐标值域位于 [1,n][1,n] 的点,其凸包的点数为 O(n2/3)O(n^{2/3})。

【证明】 考虑凸包上相邻两个横坐标相差超过 n1/3n^{1/3} 对,则只有 n2/3n^{2/3} 对;如果相邻两个纵坐标相差超过 n1/3n^{1/3} 对,则只有 n2/3n^{2/3} 对;如果均不超过,则每个至多相差 n1/3n^{1/3},且如果凸包上相邻两条边斜率相同可看作一条边,所以横纵坐标相差的所有可能只有 n1/3×n1/3=n2/3n^{1/3}\times n^{1/3}=n^{2/3} 种,故凸包的点数为 O(n2/3)O(n^{2/3}) 的。

接下来,只需要维护这个下凸包即可,每次相当于合并若干个凸包。不难做到,O(n5/3log⁡n)O(n^{5/3}\log n) 的时间复杂度。

【优化 1】:注意到,log⁡n\log n 是排序的瓶颈,不过由于值域在 [1,n][1,n] 可通过桶排轻松去掉。或者是每次合并若干个有序序列,采用归并排序的方式也可做到线性。

【优化 2】:注意到,查询的时候是 O(n2/3q)O(n^{2/3}q) 的。这很不好,所以考虑可以在凸包上三分 / 二分,即可做到 O(qlog⁡n2/3)O(q\log n^{2/3})。

§3\S 3\quad 代码

C++
#include <bits/stdc++.h>
#define fi first
#define se second
using namespace std;
using i64 = long long;
using pii = pair<int, int>;

const int maxn = 50005, maxm = 100005;

int n, m, q;
int h[maxn], e[maxm], ne[maxm], w[maxm], idx;
int din[maxn], top;
pii stk[maxn];
std::vector<pii> conv[maxn];

void add(int a, int b, int c) {
	e[idx] = b, ne[idx] = h[a], w[idx] = c, h[a] = idx ++;
}

int main() {
	cin.tie(0);
	cout.tie(0);
	ios::sync_with_stdio(0);

	cin >> n >> m;

	memset(h, -1, sizeof h);
	while (m -- ) {
		int u, v, c;
		cin >> u >> v >> c;
		add(u, v, c), din[v] ++;
	}

	queue<int> Q;
	Q.push(1), conv[1].emplace_back(0, 0);
	while (Q.size()) {
		int u = Q.front();
		Q.pop(), top = 0;
		sort(conv[u].begin(), conv[u].end());
		for (auto [x, y] : conv[u]) {
			int t = top - 1;
			if (t >= 0 && y >= stk[t].se) continue;
			while (t > 0 && (i64)(y - stk[t].se) * (stk[t].fi - stk[t - 1].fi)
					<= (i64)(stk[t].se - stk[t - 1].se) * (x - stk[t].fi))
				t --, top --;
			stk[top ++ ] = {x, y};
		}
		conv[u].clear();
		for (int i = 0; i < top; i ++)
			conv[u].push_back(stk[i]);

		for (int i = h[u]; ~i; i = ne[i]) {
			int v = e[i];
			for (auto [x, y] : conv[u])
				if (!w[i]) conv[v].emplace_back(x + 1, y);
				else conv[v].emplace_back(x, y + 1);
			if (!( -- din[v])) Q.push(v);
		}
	}

	cin >> q;
	while (q -- ) {
		int a, b, x;
		cin >> a >> b >> x;

		i64 res = 1ll << 60;
		for (auto [x, y] : conv[x])
			res = min(res, (i64)a * x + (i64)b * y);

		cout << res << '\n';
	}

	return 0;
}
原文