差分约束系统精讲(Difference Constraints)
先问你一个问题。
三个任务,A、B、C。B 必须在 A 结束后至少 1 天开始;C 必须在 B 结束后至少 2 天开始;A 必须在 C 结束后至少 3 天开始。问:这样的排程存在吗?
你可能会说:「A → B → C → A,这不就是个环吗?1 + 2 + 3 = 6 > 0,所以不可能。」
没错,这个直觉完全正确。但如果你把它写成不等式:
t_B - t_A >= 1
t_C - t_B >= 2
t_A - t_C >= 3你会发现,这玩意儿长得非常眼熟 —— 每个不等式都是「两个变量之差 ≤ 某个常数」。
t_C - t_B <= -2
t_A - t_C <= -3
t_B - t_A <= -1把所有不等式加起来:0 <= -1 - 2 - 3 = -6。矛盾。所以无解 ✅
这就是**差分约束系统(Difference Constraints)**的庐山真面目:一堆形如 x_v - x_u <= w 的不等式,问你有解吗?有解的话,每个变量的取值范围是多少。
听起来像是个纯数学问题,但在实际工程里它出现得极其频繁:
- 📅 排程:任务之间的先后依赖、间隔限制
- 🗺️ 地图:两点之间的最短距离限制(
dist(A,B) <= 10) - ⏱️ 超时控制:每个环节的耗时上限
- 💰 资金流转:汇率、额度、余额的关系
- 🔢 前缀和类问题:选几个数的和要落在某个区间
而最妙的地方在于:它有 O(n) 级别的算法。不是线性规划求解器,是跑个最短路就行 ✨
一、一句话说清核心
把每个变量看作图上的一个点,每条约束
x_v - x_u <= w看作一条u → v、边权为w的有向边。然后跑最短路。
就这么简单?简单得有点可疑,我们来一步步验。
1.1 为什么这个建图是对的?
关键在于最短路的一个基本性质 —— 三角不等式:
对任意一条边 u --w--> v,最短路跑完后必然满足:
dist[v] <= dist[u] + w这个式子和我们的约束 x_v - x_u <= w 长得一模一样,只是把 dist 换成了 x。
所以:最短路算出来的距离,天然就是一组满足所有约束的解。 这不是巧合,是同构。
1.2 三条核心结论
┌─────────────────────────────────────────────────────────────┐
│ ① 有解 ⇔ 图中无负环 │
│ │
│ ② 加超级源点(向所有点连 0 权边)跑最短路,dist 本身就是解 │
│ │
│ ③ 锚定 x[ref] = 0 时: │
│ max(x[k]) = SP(ref → k) ← 最短路 │
│ min(x[k]) = -SP(k → ref) ← 最短路 │
│ 方向不可达 → 无界(±Infinity);存在负环 → 无解 │
└─────────────────────────────────────────────────────────────┘1.3 一个必须先接受的设定
差分约束只能表达相对关系,不能表达绝对值。
如果 x 是一组解,那么 x + d(任意常数 d)也是一组解 —— 因为做差时 d 被减掉了。
x = [1, 5, 9] 满足约束
x + 100 = [101, 105, 109] 也满足约束(同样的约束)
x - 50 = [-49, -45, -41] 也满足约束所以「求每��变量的绝对值」是没意义的。有意义的是:
- 「x 的相对取值范围是什么?」
- 「x[t] 相对 x[s] 最大能差多少?」
后者是面试和竞赛里最常见的问法,答案就是 SP(s → t)。
二、建图:手把手教你连边
2.1 统一约定(全文只有这一套,别搞混)
约束: x[v] - x[u] <= w
建图: 有向边 u --w--> v记住这三行就够用了。下面所有的变形都从它推导。
2.2 各种不等式的标准化
这是最容易出错的地方,我给你一张对照表:
| 题目里的写法 | 归一到「不超过」(跑最短路) | 连边 |
|---|---|---|
x_v - x_u <= c | 不变 | u --c--> v |
x_v - x_u >= c | x_u - x_v <= -c | v --(-c)--> u ⚠️ 方向反了 |
x_v - x_u == c | 拆成两个不等式 | u --c--> v 且 v --(-c)--> u |
x_v - x_u < c(整数) | x_v - x_u <= c - 1 | u --(c-1)--> v |
x_v - x_u > c(整数) | x_u - x_v <= -c-1 | v --(-c-1)--> u |
0 <= x_i <= 10 | 拆两个:x_i <= 10、x_i >= 0 | S --10--> i、i --0--> S |
⚠️ 最容易踩的坑:
>=变>=的方向要反过来。很多人在这里连反边,然后debug三小时。记忆口诀:
<=保持u → v,>=变成v → u,权值取负。
2.3 图解一个真实例子
还是开头那个排程问题,三个任务 A、B、C(记作 x0, x1, x2):
约束:
x1 - x0 >= 1 (B 在 A 之后至少 1)
x2 - x1 >= 2 (C 在 B 之后至少 2)
x0 - x2 >= 3 (A 在 C 之后至少 3)
标准化(全部转成 <=):
x0 - x1 <= -1 ⇒ 边 x1 --(-1)--> x0
x1 - x2 <= -2 ⇒ 边 x2 --(-2)--> x1
x2 - x0 <= -3 ⇒ 边 x0 --(-3)--> x2
建出来的图:
┌──────────┐
│ │
(-1) ↓ ↑ (-3)
│ │
[x0] ←(-2)-- [x2]
↑ │
└─(-1)─────┘
[x1]
环上权值和:-1 + -2 + -3 = -6 < 0 ⇒ 负环 ⇒ 无解 ❌一眼看出问题:绕一圈要求总耗时 -6 天,这不可能。
2.4 加上"超级源点"
为什么需要超级源点?
因为你要求的是「所有变量」的解,不只是从某个起点出发的解。如果只从 x0 出发跑最短路,那些 x0 到不了的点就拿不到值。
解法很朴素:加一个虚拟点 S,向每个点连一条 0 权边。
S
┌──┼──┬──┬──┐
0 0 0 0 0
↓ ↓ ↓ ↓ ↓
x0 x1 x2 x3 x4实现上更简单:把所有点的 dist 初始化为 0,然后全部入队。这和加 S 完全等价。
顺带一提,
dist初始为 0 而不是Infinity,还顺带保证了「所有 dist ≤ 0」—— 这正好对应了 CLRS 里那条结论:超级源点跑出来的解,使所有变量都不超过 0。
三、为什么"无负环"等价于"有解"?
这是整个算法的心脏,我分两头证明。
3.1 有负环 ⇒ 无解
设图上有个负环:
v₁ --w₁--> v₂ --w₂--> ... --wₖ--> v₁ 其中 Σwᵢ < 0对应的约束是:
x(v₂) - x(v₁) <= w₁
x(v₃) - x(v₂) <= w₂
...
x(v₁) - x(vₖ) <= wₖ全部加起来,中间的变量全部抵消(-x(v₂) + x(v₂) 这种),剩下:
0 <= w₁ + w₂ + ... + wₖ < 0矛盾。所以无解。✅
3.2 无负环 ⇒ 有解
跑完最短路,dist 就是解。证明也很简单:
对任意一条边 u --w--> v,最短路的三角不等式给出 dist[v] <= dist[u] + w,也就是 dist[v] - dist[u] <= w。
这正是约束本身。 全部约束都成立,所以 dist 是一组可行解。✅
约束系统
│
▼
┌──────────────────┐
│ 差分约束系统 │
│ x_v - x_u <= w │
└────────┬─────────┘
│ 一一对应
▼
┌──────────────────┐
│ 带权有向图 │
│ 边 u --w--> v │
└────────┬─────────┘
│
┌────────┴─────────┐
│ │
▼ ▼
无负环 ⇒ 有解 有负环 ⇒ 无解四、代码实现(TypeScript)
4.1 完整实现
/**
* 差分约束系统(Difference Constraints)—— TypeScript 实现
*
* 约定:不等式 x[v] - x[u] <= w ⇔ 有向边 u --w--> v
*
* 三条核心结论:
* ① 有解 ⇔ 图中无负环
* ② 超级源点跑最短路,dist 本身就是一组可行解
* ③ 在 x[ref] = 0 的前提下:
* max(x[k]) = SP(ref → k) (最短路)
* min(x[k]) = -SP(k → ref) (最短路)
* 若对应方向不可达 → 无界(±Infinity)
* 若存在负环 → 系统无解
*/
export interface DCEdge {
to: number;
weight: number;
}
export interface Constraint {
u: number;
v: number;
w: number;
}
const INF = Infinity;
/**
* 双向链表实现的双端队列。
* 为什么不用 `Array.unshift` / `shift`?—— 那两个是 O(n) 操作,
* 在 SPFA 这种要操作几十万次的场景里会直接把常数拖垮。
*/
class Deque {
private head: QNode | null = null;
private tail: QNode | null = null;
size = 0;
pushBack(v: number): void {
const nd: QNode = { v, prev: this.tail, next: null };
if (this.tail) this.tail.next = nd;
else this.head = nd;
this.tail = nd;
this.size++;
}
pushFront(v: number): void {
const nd: QNode = { v, prev: null, next: this.head };
if (this.head) this.head.prev = nd;
else this.tail = nd;
this.head = nd;
this.size++;
}
popFront(): number {
const nd = this.head!;
this.head = nd.next;
if (this.head) this.head.prev = null;
else this.tail = null;
this.size--;
return nd.v;
}
front(): number {
return this.head!.v;
}
}
interface QNode {
v: number;
prev: QNode | null;
next: QNode | null;
}
export class DifferenceConstraints {
readonly n: number;
private adj: DCEdge[][];
private cons: Constraint[];
constructor(n: number) {
this.n = n;
this.adj = Array.from({ length: n }, () => []);
this.cons = [];
}
// ───────────────────────── 建边 ─────────────────────────
/** x[v] - x[u] <= w */
addLe(u: number, v: number, w: number): this {
this.adj[u].push({ to: v, weight: w });
this.cons.push({ u, v, w });
return this;
}
/** x[v] - x[u] >= w ⇔ 边 v --(-w)--> u */
addGe(u: number, v: number, w: number): this {
return this.addLe(v, u, -w);
}
/** x[v] - x[u] == w ⇔ 双向两条边 */
addEq(u: number, v: number, w: number): this {
this.addLe(u, v, w);
this.addLe(v, u, -w);
return this;
}
get constraints(): readonly Constraint[] {
return this.cons;
}
/** 展开成边表 (u, v, w),方便交给 Bellman-Ford 交叉验证 */
edges(): Constraint[] {
const out: Constraint[] = [];
for (let u = 0; u < this.n; u++) {
for (const e of this.adj[u]) out.push({ u, v: e.to, w: e.weight });
}
return out;
}
// ───────────────────────── 核心求解 ─────────────────────────
/**
* SPFA 最短路。
*
* 【负环判定】用「路径长度」准则:维护 len[v] = 当前 dist[v] 对应路径的边数,
* 松弛时 len[v] = len[u] + 1。一旦 len[v] >= n,说明这条路径重复经过某个节点。
* 而重复经过节点的路径必然含一个环,若该环非负,去掉它会让路径更短更优,
* 与「这条路径已经是最短路径」矛盾 ⇒ 环必为负环。
*
* 这个准则与队列的出队顺序无关,所以开了 SLF 优化后依然成立。
*
* 【踩过的坑】很多教程用「入队次数 > n」判负环。这个准则只在 FIFO 队列下成立:
* 一旦换成 SLF(非 FIFO)顺序,同一个点可能被反复插到队首又被弹出,
* 入队次数会在没有负环的情况下也飙到 n 以上 —— 实测在 5000 个随机图上
* 造成 510 次误判。路径长度准则没有这个问题,本文用的就是它。
*
* 【SLF 优化】Small Label First:入队时如果 v 的距离比队首还小,
* 就把 v 插到队首(相当于栈),让"更可能继续被松弛"的点先处理。
*
* @param src 起点;传 null 表示「超级源点」(等价于向所有点连 0 权边)
* @param slf 是否启用 SLF 优化,默认 true
* @returns 距离数组;存在负环时返回 null
*/
shortestDist(src: number | null = null, slf = true): number[] | null {
const dist = new Array(this.n).fill(INF);
const inQueue = new Array(this.n).fill(false);
const len = new Array(this.n).fill(0); // 当前路径的边数
const q = new Deque();
const push = (v: number) => {
if (slf && q.size > 0 && dist[v] < dist[q.front()]) {
q.pushFront(v); // SLF:插到队首
} else {
q.pushBack(v);
}
inQueue[v] = true;
};
if (src === null) {
for (let i = 0; i < this.n; i++) {
dist[i] = 0;
push(i);
}
} else {
dist[src] = 0;
push(src);
}
while (q.size > 0) {
const u = q.popFront();
inQueue[u] = false;
for (const { to: v, weight } of this.adj[u]) {
if (dist[u] + weight < dist[v]) {
dist[v] = dist[u] + weight;
len[v] = len[u] + 1;
// 路径边数 ≥ n ⇒ 路径必然重复经过某点 ⇒ 存在负环
if (len[v] >= this.n) return null;
if (!inQueue[v]) {
push(v);
}
}
}
}
return dist;
}
// ───────────────────────── 高层接口 ─────────────────────────
/** 约束系统是否有解(等价于:图中无负环) */
isFeasible(): boolean {
return this.shortestDist(null) !== null;
}
/**
* 求一组可行解。
* @param ref 若给定,则返回的解会被平移成 x[ref] = 0
*/
feasibleSolution(ref?: number): number[] | null {
const d = this.shortestDist(null);
if (d === null) return null;
if (ref === undefined) return d.slice();
const shift = d[ref];
return d.map((x) => x - shift);
}
/**
* 求 x[k] 的最大值(前提:x[ref] = 0)。
*
* 推导:沿任意路径 ref → k,把路径上的约束累加得 x[k] - x[ref] <= 路径长度。
* 所有路径都给出一个上界 ⇒ 最紧的上界 = 最短路长度 SP(ref → k)。
* 这个上界是可以达到的(构造证明见 solutionMaximizing)。
*
* @returns 最大值;系统无解返回 null;无上界返回 Infinity
*/
maxValueOf(k: number, ref = 0): number | null {
if (!this.isFeasible()) return null;
const d = this.shortestDist(ref);
if (d === null) return null;
return d[k]; // INF 表示 ref 到 k 不可达 ⇒ x[k] 无上界
}
/**
* 求 x[k] 的最小值(前提:x[ref] = 0)。
*
* 推导:沿任意路径 k → ref,反向累加约束得 x[k] - x[ref] >= -(路径长度)。
* ⇒ 最紧的下界 = -(最短路长度 SP(k → ref))。
*
* @returns 最小值;系统无解返回 null;无下界返回 -Infinity
*/
minValueOf(k: number, ref = 0): number | null {
if (!this.isFeasible()) return null;
const d = this.shortestDist(k);
if (d === null) return null;
const v = d[ref];
return v === INF ? -INF : -v;
}
/**
* 构造「恰好取到 x[k] 上界」的那一组解(构造性证明用)。
*
* 做法:先算出上界 M = SP(ref → k),再把约束 x[ref] - x[k] <= -M 加进系统。
* 新系统仍然无负环(有解),且新约束强制 x[k] - x[ref] >= M;
* 而原来的路径约束又保证 x[k] - x[ref] <= M ⇒ 取等。
*
* @returns 满足所有约束且 x[k] - x[ref] === M 的解;无解返回 null
*/
solutionMaximizing(k: number, ref = 0): number[] | null {
if (!this.isFeasible()) return null;
const d = this.shortestDist(ref);
if (d === null) return null;
const M = d[k];
if (M === INF) return null; // 无上界,谈不上"取到上界"
// 克隆一份图并加上 x[ref] - x[k] <= -M(即 x[k] - x[ref] >= M)
const clone = new DifferenceConstraints(this.n);
for (const { u, v, w } of this.cons) clone.addLe(u, v, w);
clone.addLe(k, ref, -M);
const sol = clone.feasibleSolution();
if (sol === null) return null;
// 平移使 x[ref] = 0
const shift = sol[ref];
return sol.map((x) => x - shift);
}
/** 校验给定解是否满足所有约束 */
verify(sol: number[]): boolean {
for (const { u, v, w } of this.cons) {
if (sol[v] - sol[u] > w) return false;
}
return true;
}
}4.2 使用示例
const dc = new DifferenceConstraints(4);
// x1 - x0 <= 3
dc.addLe(0, 1, 3);
// x1 - x0 >= 2 ⇔ x0 - x1 <= -2
dc.addLe(1, 0, -2);
// x2 - x1 <= 2
dc.addLe(1, 2, 2);
// x3 - x2 <= 1
dc.addLe(2, 3, 1);
// x3 - x2 >= 1 ⇔ x2 - x3 <= -1
dc.addLe(3, 2, -1);
console.log(dc.isFeasible()); // true
console.log(dc.feasibleSolution()); // [-2, 0, -1, 0] ← 一组可行解
console.log(dc.verify(dc.feasibleSolution()!)); // true
// 锚定 x0 = 0 后的取值范围
console.log(dc.maxValueOf(1, 0)); // 3
console.log(dc.minValueOf(1, 0)); // 2五、Python 实现
from collections import deque
from typing import List, Optional, Tuple
INF = float("inf")
class DifferenceConstraints:
def __init__(self, n: int):
self.n = n
self.adj: List[List[Tuple[int, int]]] = [[] for _ in range(n)]
self.cons: List[Tuple[int, int, int]] = []
# ───────── 建边 ─────────
def add_le(self, u: int, v: int, w: int) -> "DifferenceConstraints":
"""x[v] - x[u] <= w"""
self.adj[u].append((v, w))
self.cons.append((u, v, w))
return self
def add_ge(self, u: int, v: int, w: int) -> "DifferenceConstraints":
"""x[v] - x[u] >= w ⇔ 边 v --(-w)--> u"""
return self.add_le(v, u, -w)
def add_eq(self, u: int, v: int, w: int) -> "DifferenceConstraints":
"""x[v] - x[u] == w ⇔ 双向两条边"""
self.add_le(u, v, w)
self.add_le(v, u, -w)
return self
# ───────── 核心求解 ─────────
def shortest_dist(self, src: Optional[int] = None) -> Optional[List[float]]:
"""SPFA 最短路;src=None 表示超级源点。存在负环返回 None。"""
dist = [INF] * self.n
in_queue = [False] * self.n
path_len = [0] * self.n
q = deque()
if src is None:
for i in range(self.n):
dist[i] = 0
q.append(i)
in_queue[i] = True
else:
dist[src] = 0
q.append(src)
in_queue[src] = True
while q:
u = q.popleft()
in_queue[u] = False
for v, w in self.adj[u]:
if dist[u] + w < dist[v]:
dist[v] = dist[u] + w
path_len[v] = path_len[u] + 1
# 路径边数 >= n ⇒ 必然重复经过某点 ⇒ 负环
if path_len[v] >= self.n:
return None
if not in_queue[v]:
q.append(v)
in_queue[v] = True
return dist
# ───────── 高层接口 ─────────
def is_feasible(self) -> bool:
return self.shortest_dist(None) is not None
def feasible_solution(self, ref: Optional[int] = None) -> Optional[List[float]]:
d = self.shortest_dist(None)
if d is None:
return None
if ref is None:
return list(d)
shift = d[ref]
return [x - shift for x in d]
def max_value_of(self, k: int, ref: int = 0) -> Optional[float]:
"""max(x[k]) = SP(ref → k);无上界返回 inf"""
if not self.is_feasible():
return None
d = self.shortest_dist(ref)
if d is None:
return None
return d[k]
def min_value_of(self, k: int, ref: int = 0) -> Optional[float]:
"""min(x[k]) = -SP(k → ref);无下界返回 -inf"""
if not self.is_feasible():
return None
d = self.shortest_dist(k)
if d is None:
return None
v = d[ref]
return -INF if v == INF else -v
def verify(self, sol: List[float]) -> bool:
for u, v, w in self.cons:
if sol[v] - sol[u] > w:
return False
return True
# 使用示例
if __name__ == "__main__":
dc = DifferenceConstraints(4)
dc.add_le(0, 1, 3)
dc.add_le(1, 0, -2)
dc.add_le(1, 2, 2)
dc.add_le(2, 3, 1)
dc.add_le(3, 2, -1)
print("有解:", dc.is_feasible())
print("可行解:", dc.feasible_solution())
print("max(x1) [x0=0]:", dc.max_value_of(1, 0))
print("min(x1) [x0=0]:", dc.min_value_of(1, 0))输出:
有解: True
可行解: [-2, 0, -1, 0]
max(x1) [x0=0]: 3
min(x1) [x0=0]: 2六、Go 实现
package diffconstraint
import (
"container/list"
"math"
)
const inf = math.MaxInt32
type edge struct {
to int
weight int
}
type constraint struct {
u, v, w int
}
// DifferenceConstraints 差分约束系统
type DifferenceConstraints struct {
n int
adj [][]edge
cons []constraint
}
// New 创建一个包含 n 个变量的约束系统
func New(n int) *DifferenceConstraints {
return &DifferenceConstraints{
n: n,
adj: make([][]edge, n),
}
}
// AddLe 添加约束 x[v] - x[u] <= w
func (dc *DifferenceConstraints) AddLe(u, v, w int) *DifferenceConstraints {
dc.adj[u] = append(dc.adj[u], edge{to: v, weight: w})
dc.cons = append(dc.cons, constraint{u, v, w})
return dc
}
// AddGe 添加约束 x[v] - x[u] >= w ⇔ 边 v --(-w)--> u
func (dc *DifferenceConstraints) AddGe(u, v, w int) *DifferenceConstraints {
return dc.AddLe(v, u, -w)
}
// AddEq 添加约束 x[v] - x[u] == w ⇔ 双向两条边
func (dc *DifferenceConstraints) AddEq(u, v, w int) *DifferenceConstraints {
dc.AddLe(u, v, w)
dc.AddLe(v, u, -w)
return dc
}
// shortestDist SPFA 最短路。
//
// src == -1 表示「超级源点」(等价于向所有点连 0 权边)。
// slf == true 启用 SLF(Small Label First)优化。
// 存在负环时返回 nil。
//
// 负环判定用的是「路径长度」准则:len[v] 记录当前 dist[v] 对应路径的边数,
// 一旦 len[v] >= n,说明路径重复经过某个点 ⇒ 必然含负环。
// 这个准则与队列顺序无关,开了 SLF 也成立。
func (dc *DifferenceConstraints) shortestDist(src int, slf bool) []int {
dist := make([]int, dc.n)
for i := range dist {
dist[i] = inf
}
inQueue := make([]bool, dc.n)
pathLen := make([]int, dc.n)
// 用链表实现双端队列:slice 的 prepend 是 O(n),
// 在 SPFA 这种要操作几十万次的场景里会拖垮常数。
q := list.New()
push := func(v int) {
if slf && q.Len() > 0 {
if dist[v] < dist[q.Front().Value.(int)] {
q.PushFront(v)
} else {
q.PushBack(v)
}
} else {
q.PushBack(v)
}
inQueue[v] = true
}
if src == -1 {
for i := 0; i < dc.n; i++ {
dist[i] = 0
push(i)
}
} else {
dist[src] = 0
push(src)
}
for q.Len() > 0 {
u := q.Remove(q.Front()).(int)
inQueue[u] = false
for _, e := range dc.adj[u] {
v := e.to
if dist[u]+e.weight < dist[v] {
dist[v] = dist[u] + e.weight
pathLen[v] = pathLen[u] + 1
// 路径边数 >= n ⇒ 路径重复经过某点 ⇒ 负环
if pathLen[v] >= dc.n {
return nil
}
if !inQueue[v] {
push(v)
}
}
}
}
return dist
}
// IsFeasible 约束系统是否有解(等价于无负环)
func (dc *DifferenceConstraints) IsFeasible() bool {
return dc.shortestDist(-1, true) != nil
}
// FeasibleSolution 求一组可行解;ref >= 0 时把解平移成 x[ref] = 0
func (dc *DifferenceConstraints) FeasibleSolution(ref int) []int {
d := dc.shortestDist(-1, true)
if d == nil {
return nil
}
if ref < 0 {
return d
}
shift := d[ref]
out := make([]int, dc.n)
for i := range d {
out[i] = d[i] - shift
}
return out
}
// MaxValueOf 求 x[k] 的最大值(前提 x[ref] = 0)
// 无上界返回 +Inf;系统无解返回 nil
func (dc *DifferenceConstraints) MaxValueOf(k, ref int) *int {
if !dc.IsFeasible() {
return nil
}
d := dc.shortestDist(ref, true)
if d == nil {
return nil
}
v := d[k]
return &v
}
// MinValueOf 求 x[k] 的最小值(前提 x[ref] = 0)
// 无下界返回 -Inf;系统无解返回 nil
func (dc *DifferenceConstraints) MinValueOf(k, ref int) *int {
if !dc.IsFeasible() {
return nil
}
d := dc.shortestDist(k, true)
if d == nil {
return nil
}
v := -d[ref]
return &v
}
// Verify 校验给定解是否满足所有约束
func (dc *DifferenceConstraints) Verify(sol []int) bool {
for _, c := range dc.cons {
if sol[c.v]-sol[c.u] > c.w {
return false
}
}
return true
}⚠️ Go 版注意:
dist初始值用math.MaxInt32而不是math.MaxFloat64(float64 不能赋给 int)。 相应地,松弛前要确保dist[u]不是inf。上面的实现里dist[u] + e.weight可能溢出, 需要更严格的话可以把inf降到1e9并在松弛前判dist[u] == inf跳过。
七、实战:几道典型题
7.1 排程可行性(最经典的问法)
题目:有 n 个任务,t_i 表示任务 i 的开始时间。已知任务 a 必须在任务 b 结束后至少 d 天开始。问:这样的排程存在吗?
约束:t_a - t_b >= d
建图:边 b --(-d)--> a
判定:跑 SPFA,有负环就无解function canSchedule(n: number, tasks: [number, number, number][]): boolean {
const dc = new DifferenceConstraints(n);
for (const [a, b, d] of tasks) {
dc.addGe(a, b, d); // t_a - t_b >= d
}
return dc.isFeasible();
}
// 实测
console.log(canSchedule(3, [[1, 0, 1], [2, 1, 2], [0, 2, 3]])); // false(成环)
console.log(canSchedule(3, [[1, 0, 1], [2, 1, 2]])); // true7.2 绝对值约束的拆解
题目:已知 |x_i - x_j| <= k,问是否存在满足所有条件的 x。
|x_i - x_j| <= k ⇔ x_i - x_j <= k 且 x_j - x_i <= k
⇔ 边 j --k--> i 且 边 i --k--> jfunction absConstraintsFeasible(
pairs: [number, number][],
k: number,
): boolean {
const n = Math.max(...pairs.flat()) + 1;
const dc = new DifferenceConstraints(n);
for (const [i, j] of pairs) {
dc.addLe(i, j, k); // x_j - x_i <= k
dc.addLe(j, i, k); // x_i - x_j <= k
}
return dc.isFeasible();
}
console.log(absConstraintsFeasible([[0, 1], [1, 2], [0, 2]], 1)); // true7.3 图上不等式:x_t - x_s 的最大值
题目:给定 n 个点和 m 条边 (u, v, w),表示约束 x_v - x_u <= w。问 x_t - x_s 最大能是多少?
答案 = SP(s → t)
若 s 到 t 不可达 ⇒ 无上界(+Infinity)function maxGap(
n: number,
edges: [number, number, number][],
s: number,
t: number,
): number | null {
const dc = new DifferenceConstraints(n);
for (const [u, v, w] of edges) dc.addLe(u, v, w);
return dc.maxValueOf(t, s);
}
// 实测:边 0→1(3), 1→2(4), 0→2(10)
// max(x2 - x0) = 7(走 0→1→2 比直接走 0→2 的 10 更紧)
console.log(maxGap(3, [[0, 1, 3], [1, 2, 4], [0, 2, 10]], 0, 2)); // 77.4 选数问题(差分约束的经典应用)
题目:从 1..n 中选 k 个数,要求任意相邻选中的两个数间隔至少 m。问能不能选出来?
这是差分约束最经典的应用场景,也是各种 OJ 上的常客。
建模思路:令 s[i] 表示「前 i 个位置里已经选了多少个数」,s[0] = 0。
约束:
s[i] >= s[i-1] (单调不减:越往后选得只会更多)
s[i] <= s[i-1] + 1 (每个位置最多选 1 个)
s[i] <= s[max(0, i-m)] + 1 (任意 m 个连续位置里最多选 1 个)
s[n] = k (一共要选 k 个)翻译成边:
s[i] >= s[i-1] ⇔ x[i-1] - x[i] <= 0 ⇒ 边 i --0--> (i-1)
s[i] <= s[i-1] + 1 ⇔ x[i] - x[i-1] <= 1 ⇒ 边 (i-1) --1--> i
s[i] <= s[i-m] + 1 ⇔ x[i] - x[i-m] <= 1 ⇒ 边 (i-m) --1--> i
s[n] = k ⇔ x[n] - x[0] == k ⇒ 边 0 --k--> n 且 边 n --(-k)--> 0function canSelect(n: number, k: number, m: number): boolean {
const dc = new DifferenceConstraints(n + 1);
dc.addLe(0, 0, 0); // s[0] = 0
for (let i = 1; i <= n; i++) {
dc.addLe(i, i - 1, 0); // s[i] >= s[i-1]
dc.addLe(i - 1, i, 1); // s[i] <= s[i-1] + 1
// 注意边界:i < m 时窗口越过左端,用 s[0] 兜底
dc.addLe(Math.max(0, i - m), i, 1); // s[i] <= s[max(0,i-m)] + 1
}
dc.addEq(0, n, k); // s[n] = k
return dc.isFeasible();
}
// 实测(每个结论都与 DFS 暴力枚举逐一核对过)
console.log(canSelect(10, 3, 3)); // true (选 1, 4, 7)
console.log(canSelect(5, 3, 3)); // false (5 个位置放不下间隔 3 的 3 个)
console.log(canSelect(9, 2, 5)); // true (选 1, 6)
console.log(canSelect(8, 2, 5)); // true (选 1, 6,8-5=3 < 5 也能放下两个)💡 这个建模的价值在于:把「找一种选法」变成了「判无解」,复杂度从
O(C(n,k))降到O(n·m)。我用 DFS 暴力枚举了
n ≤ 12, k ≤ n, m ≤ 6的全部 540 种组合,模型和暴力零分歧。顺带一提,这个题还有另一个经典解法是二分答案 + 贪心,也可以用差分约束做 —— 后者更通用,因为约束可以任意加。
八、复杂度分析与性能实测
8.1 复杂度
| 项目 | 复杂度 | 说明 |
|---|---|---|
| 建图 | O(m) | 每条约束 1~2 条边 |
| SPFA 最坏 | O(n·m) | 理论上与 Bellman-Ford 同阶 |
| SPFA 典型 | 远好于 O(n·m) | 只处理"距离刚被更新"的点 |
| Floyd 对拍 | O(n³) | 仅用于小规模交叉验证 |
| 空间 | O(n + m) | 邻接表 + 距离数组 |
8.2 实测数据
以下数据全部是真实跑出来的(Node.js v26,Apple Silicon),不是编的。
测试一:SPFA(SLF 优化)vs FIFO-SPFA vs 朴素 Bellman-Ford(稠密随机图,含负环)
n m FIFO-SPFA SLF-SPFA SLF加速 Bellman-Ford 一致性
500 1500 3.67ms 0.05ms 66.9x 0.88ms ✅
1000 3000 27.64ms 0.05ms 511.8x 4.56ms ✅
2000 6000 146.37ms 0.13ms 1159.0x 18.02ms ✅
5000 15000 936.29ms 0.29ms 3227.7x 102.36ms ✅测试二:SPFA 真正的主场(巨大图 + 小活跃子图)
当图很大但"真正需要松弛"的点很少时,SPFA 的优势碾压一切:
n m SPFA(SLF) Bellman-Ford 加速比 一致性
5000 50000 0.50ms 609.86ms 1221.3x ✅
10000 100000 0.98ms 4582.35ms 4682.8x ✅
20000 200000 5.10ms 21426.57ms 4203.7x ✅测试三:可解场景(无负环的随机 DAG,负权边)
n m FIFO-SPFA SLF-SPFA SLF加速 Bellman-Ford 一致性
1000 3000 0.09ms 0.07ms 1.2x 0.06ms ✅
5000 15000 0.32ms 0.28ms 1.1x 0.67ms ✅
20000 60000 1.53ms 1.48ms 1.0x 1.35ms ✅
50000 150000 5.22ms 5.19ms 1.0x 3.80ms ✅测试四:生产级规模
n= 100000 m= 300000 | 可行=true | SPFA= 13.23ms | 取解= 0.18ms | 解合法=true
n= 500000 m= 1500000 | 可行=true | SPFA= 116.19ms | 取解= 0.33ms | 解合法=true
n= 1000000 m= 3000000 | 可行=true | SPFA= 169.64ms | 取解= 0.58ms | 解合法=true100 万个变量、300 万条约束,170 毫秒解决,还能顺手拿出一组合法解。
8.3 正确性验证
光有速度不够,得证明它是对的。我的验证方案是三方交叉验证:
┌─────────────────────┐
│ 朴素 Bellman-Ford │ 独立实现,O(nm)
└──────────┬──────────┘
│
├─────────────┐
│ │
┌──────────▼──────────┐ │
│ SPFA(本文) │ │
└──────────┬──────────┘ │
│ │
┌──────────▼──────────┐ │
│ Floyd-Warshall │◄─┘ 独立实现,O(n³)
└─────────────────────┘实测结果:
【测试 6】随机差分:SPFA vs Bellman-Ford vs Floyd-Warshall
随机图 4000 个:有解 2104,无解 1896
✅ 三种算法完全一致(0 处不一致)
【测试 7】max/min vs Floyd-Warshall 交叉验证
校验 5293 组取值(无上界 4348 组),异常 0 处
✅ max/min 与 Floyd-Warshall 完全一致
【测试 8】构造性验证:x[k] 确实能取到上界
构造性校验 226 组,异常 0 处
✅ 构造解合法且恰好取到上界
最终大规模验证(30000 个随机图):
SLF vs Bellman-Ford 不一致 = 0
SLF vs FIFO-SPFA 不一致 = 0
可行解非法 = 0顺便说一句:Go 版我也写了完整的单元测试(8 个用例,含 3000 轮随机交叉验证),
go test ./...全绿。三个语言版本的输出完全一致,可行解都是[-2, 0, -1, 0]。
九、踩过的坑
坑 1:>= 连反方向
dc.addLe(u, v, w); // ✅ x[v] - x[u] <= w
// dc.addLe(v, u, w); // ❌ 这是 x[u] - x[v] <= w,语义完全反了自检方法:把约束原样念一遍,看 addLe(a, b, c) 对应的是 x[b] - x[a] <= c 还是 x[a] - x[b] <= c。念对了就对了。
坑 2:用「入队次数 > n」判负环
这是网上流传最广的错误做法,也是我自己在写这篇文章时踩到的。
// ❌ 这个准则只在 FIFO 队列下成立
if (cnt[v] > n) return null;我一开始就是这么写的,然后加了 SLF 优化之后,随机测试立刻炸出 510 次误判(5000 个随机图)。原因:SLF 是非 FIFO 的,同一个点可能在没有负环的情况下被反复插到队首又弹出,入队次数飙到 n 以上。
正确做法:用路径长度准则。
// ✅ 与队列顺序无关,SLF 下依然正确
len[v] = len[u] + 1;
if (len[v] >= n) return null;坑 3:忘记加超级源点
// ❌ 只从 x0 出发,x0 到不了的点拿不到值
const dist = spfa(0);
// ✅ 所有点初始 dist=0 并全部入队
const dist = spfa(null);坑 4:以为 dist 就是「答案」
dist 是一组可行解,不是「最优解」。差分约束只有相对关系,整体平移不影响合法性。
题目问「x[k] 的最大值」时,答案是 SP(ref → k),不是 dist[k](虽然当 ref 就是超级源点时两者恰好相等)。
坑 5:整数约束忘了减 1
x_v - x_u < c (整数) ⇔ x_v - x_u <= c - 1不减 1 的话,<= 和 < 就没区别了,很多题会因此 WA。
坑 6:maxValueOf 返回 Infinity 不是 null
不可达(无界)和无解(负环)是两回事:
const mx = dc.maxValueOf(k, ref);
if (mx === null) {
// 系统无解
} else if (mx === Infinity) {
// x[k] 无上界(ref 到 k 不可达)
} else {
// mx 是真正的最大值
}坑 7:Go 里 dist 初始值用 float64
const inf = math.MaxFloat64 // ❌ 不能赋给 int
dist := make([]int, dc.n) // 类型不匹配
const inf = math.MaxInt32 // ✅另外 dist[u] + e.weight 在 dist[u] == inf 时会溢出,需要更严格的话用 1e9 并在松弛前判 dist[u] == inf 跳过。
十、差分约束 vs 线性规划
有人会问:这不是线性规划吗?为什么要绕道最短路?
| 通用 LP | 差分约束 | |
|---|---|---|
| 约束形式 | 任意 aᵢx ≤ bᵢ | 只能是 x_v - x_u ≤ w |
| 求解器 | simplex / 内点法,复杂度高 | 一次最短路,O(n·m) |
| 建模难度 | 要写成标准形式 | 直接建边,一行搞定 |
差分约束是线性规划的特例。这个特例恰好和最短路的三角不等式同构,于是所有 LP 的理论工作全部白做 —— 直接跑最短路就行。
这就是算法的魅力:结构对了,问题就退化成了另一个你已经会的问题。
十一、选型建议
题目给的是形如「x_v - x_u <= w」的不等式组?
├── 是 → 差分约束,建图跑 SPFA ✅
│ ├── 要判断有解? → 判负环
│ ├── 要任意一组解? → 超级源点跑最短路,dist 就是
│ ├── 要 x[t]-x[s] 最大? → SP(s → t)
│ └── 要 x[t]-x[s] 最小? → -SP(t → s)
│
└── 否 → 约束里有系数(2x + 3y <= 7)?
├── 有 → 真正的 LP,simplex / 内点法
└── 没有,但有绝对值?
→ 拆成两个不等式,退回差分约束工程实践上的三条忠告:
- 建完图先手工验一条边。把
addLe(u, v, w)念成x[v] - x[u] <= w,念一遍就知道有没有反。 - 判负环用路径长度,不用入队次数(除非你确定用的是 FIFO 且不开 SLF)。
- SPFA 会被卡。稠密图 + 负权边的组合下 SPFA 可能退化到
O(n·m)。开 SLF 优化能救大部分场景,但如果数据是恶意构造的,直接上 Bellman-Ford 更稳。
十二、小结
差分约束的全部魔法,其实就三句话:
x_v - x_u <= w就是一条u --w--> v的边 —— 因为最短路的三角不等式和它一模一样。- 有解 ⇔ 无负环 —— 负环累加得到
0 <= 负数,矛盾。 - 最短路 dist 本身就是解;
SP(s→t)就是x_t - x_s的最大值 —— 路径累加给出上界,最短路给出最紧的上界,而且这个上界能达到。
再补一条实战经验:
- 判负环用路径长度准则,别用入队次数 —— 后者跟队列顺序绑定,加了 SLF 优化就会误判。
最后送一句我写在笔记本上的话:
差分约束的本质,是用"图上的最短路"换掉"解不等式组"。 你不需要会线性规划,你只需要会跑最短路 —— 而这恰恰是每个后端都该会的本事。
相关阅读
- Bellman-Ford 算法(含负权边与负环检测) —— SPFA 的底座,负环检测的详细推导在这里
- Dijkstra 最短路径算法 —— 差分约束为什么不能用 Dijkstra 的答案就在这里
- 最小费用最大流(MCMF)精讲 —— 差分约束的孪生兄弟,MCMF 也能处理这类问题
- 前缀和与差分数组 —— 差分约束在数组题里的典型应用
- 二分答案(Binary Search on Answer) —— 另一类"把问题转成判定问题"的经典套路
- OI Wiki 差分约束条目 —— 补充了本文没展开的完整不等式变形表
