差分约束

· · 个人记录

差分约束

什么是差分约束?

差分约束,就是有约束的差分。给定一个不等式组,告诉你任意两项的差不大于/不小于一个值,最后求出这个不等式组的解。

差分约束的代码实现

差分约束的代码用到了数形结合的思想。
已知题目条件 a_i - a_j \le x ,那么移项就可以得到:a_i \le x + a_j ,而这个式子与图论中单源最短路径算法中边的松弛不等式一致。
这时,我们就会想到建图,其中连接 i 号节点与 j 号节点的边权为 x 。那么容易发现: dis[i] - dis[j] \le x,而 dis 数组刚好就是我们需要求得的那组解。

>差分约束系统是 $“$ 用图论的语言重新描述代数约束,并用最短路径算法来解决不等式求解 $”$ 的经典跨领域思想体现。 在计算 $dis$ 数组时,我们通常会使用 $SPFA$ 算法,复杂度优于 $Bellman-Ford$ 。同时,计算过程中还需要判断负环(无解的情况)。一般情况下,还需要引入$“$ 超级源点 $”$ 来连通整个图,遍历也一般从源点开始。 原理知道了,那么代码实现就会很容易了。 :::info[代码框架] ```cpp line-numbers int head[N], ver[N], ne[N], w[N], tot;//链式前向星存储图 void add(int x, int y, int z){ ver[++tot] = y; w[tot] = z; ne[tot] = head[x]; head[x] = tot; } int dis[N]; //计算距离 int cnt[N]; //统计每个节点的入队次数 bool vis[N]; //判断当前节点是否在队列里 bool spfa(){ memset(dis, 0x3f, sizeof dis); //初始化大小为正无穷 queue<int> q; q.push(0); //0为超级源点 dis[0] = 0; //0的初始距离为0 vis[0] = 1; //0在队列中 while(!q.empty()){ int tp = q.front(); q.pop(); //tp为队首数字 vis[tp] = 0; //弹出队首,队首节点不在队列内部 for(int i = head[tp]; i; i = ne[i]){ //链式前向星遍历队首节点能到达的每条边 int y = ver[i]; //队首节点能到达的每个点 if(dis[y] <= dis[tp] + w[i]) continue; dis[y] = dis[tp] + w[i]; //计算dis数组 cnt[y] ++; //节点入队次数+1 if(cnt[y] > n){ //出队次数大于节点数,有负环,无解 return false; } if(vis[y]) continue; //如果当前节点已经在队列当中就不入队 vis[y] = 1; q.push(y); } } return true; } ``` ::: --- [例](https://bbcoj.cn/training/293/problem/ST16LDX/full-screen) ([洛谷传送门](https://www.luogu.com.cn/problem/P5960)) :::info[题目大意] 给定 $n$ 个变量 $x_1$ , $x_2$ , ..., $x_n$ 和 $m$ 条约束条件,每条约束形如 $x_\alpha - x_\beta ≤ c$。判断是否存在满足所有约束的可行解,若存在则输出任意一组解,若不存在则输出 $“NO”$。 ::: :::warning[题目思路] 根据前面讲述的思路,建图,求最短路径 ::: :::success[ACcode?] ```cpp line-numbers #include<bits/stdc++.h> using namespace std; int n, m; int dis[N]; int cnt[N]; bool vis[N]; //链式前向星 void spfa(){ //初始化 while(!q.empty()){ int tp = q.front(); q.pop(); vis[tp] = 0; for(int i = head[tp]; i; i = ne[i]){ int y = ver[i]; if(dis[y] <= dis[tp] + w[i]) continue; dis[y] = dis[tp] + w[i]; cnt[y] ++; if(cnt[y] > n){ cout << "NO"; exit(0); } if(vis[y]) continue; vis[y] = 1; q.push(y); } } } int main(){ //输入 //建图 spfa(); //输出 return 0; } ``` ::: --- ### 各种不等式的转化方法 在实际运用中,我们发现约束条件会有其他的情况,下面是不同种类的建图方法: |约束条件|条件变形|建图方式|对应算法模型| |:-:|:-:|:-:|:-:| |$a_i - a_j \le x$|$a_i \le x + a_j$|添加有向边 $j \to i$,边权为 $x$ |最短路模型| |$a_i - a_j \ge x$|$a_i \ge x + a_j$|添加有向边 $i \to j$,边权为 $x$ |最长路模型| |$a_i = a_j$|$a_i \le a_j$ 且 $a_i \ge a_j$|添加双向边 $i \to j$,边权为 $0$|通用模型| |$a_i \le x$|$a_i \le x + a_0(a_0 = 0)$|添加超级源点 $0 \to i$,权值为 $c$|最短路模型| |$k \times a_i \ge x$|$lna_i + ln(k) \ge lnx_j$|添加有向边 $i \to j$,权值为 $ln(k)$|最长路模型| [例2](https://bbcoj.cn/training/293/problem/ST16XL6/full-screen) ([洛谷传送门](https://www.luogu.com.cn/problem/P1993)) :::info[题目大意] 农场主小K 有 $n$ 块田地和 $m$ 条约束信息,分为三类: $a$ 比 $b$ 至少多 $c$ 、 $a$ 比 $b$ 至多多 $c$ 、 $a$ 与 $b$ 数量相等。判断这些约束条件是否存在矛盾。 ::: :::warning[题目思路] 根据不同的约束条件按照不同的方式建图,再判断图中有没有负环 ::: :::success[ACcode?] ```cpp line-numbers #include<bits/stdc++.h> using namespace std; const int N = 10005; int n, m; int dis[N]; int cnt[N]; bool vis[N]; //链式前向星 void spfa(){ //初始化 while(!q.empty()){ int tp = q.front(); q.pop(); vis[tp] = 0; for(int i = head[tp]; i; i = ne[i]){ int y = ver[i]; if(dis[y] <= dis[tp] + w[i]) continue; dis[y] = dis[tp] + w[i]; cnt[y] ++; if(cnt[y] > n){ cout << "No"; exit(0); } if(vis[y]) continue; vis[y] = 1; q.push(y); } } } int main(){ //输入 //建图 spfa(); //输出 return 0; } ``` ::: --- [例3](https://bbcoj.cn/training/293/problem/ST164G5/full-screen) ([洛谷传送门](https://www.luogu.com.cn/problem/P1250)) :::info[题目大意] 一条街有 $n$ 个位置,给出 $m$ 个区间 $[l, r]$,要求每个区间内至少种 $t$ 棵树,且每个位置最多种一棵树。请计算满足所有要求的情况下,最少需要种多少棵树? ::: :::warning[题目思路] 将 $dis$ 数组看为前 $i$ 个位置需要种树的总数(即前缀和),将 $“$区间 $[l, r]$ 中至少t棵树$”$ 转化为 $dis_r - dis_{l - 1} \ge t$。按照 $SPFA$ 的方法计算 $dis$ 数组,最后输出 $dis_n$ 即可。 ::: :::success[ACcode?] ```cpp line-numbers #include<bits/stdc++.h> using namespace std; int n, m; //链式前向星 int dis[N]; bool vis[N]; void spfa(){ //初始化 while(!q.empty()){ int x = q.front(); q.pop(); vis[x] = 0; for(int i = head[x]; i; i = ne[i]){ int y = ver[i]; if(dis[y] >= dis[x] + w[i]) continue; dis[y] = dis[x] + w[i]; if(vis[y]) continue; vis[y] = 1; q.push(y); } } } int main(){ //输入 //建图 spfa(); //输出 return 0; } ``` ::: --- :::align{center} $\Huge{Thank}$ $\Huge{for}$ $\Huge{your}$ $\Huge{watching!!!}

:::