解不等式组
xi <= yj + ck
就比如
x1 <= x2 + 1
x2 <= x3 + 2
x3 <= x1 - 2
x1 = 0
x2 = -1
x3 = -2
这就是其中一个可行解
那么就可以把 不等式组 变成 可行解
xi <= xj + ck
表示有一条 j -> i 权值为 ck 的边,
根据不等式组,可以建图,
求不等式组的可行解
源点需要满足的条件:从源点出发,一定可以走到所有的边
如果不能遍历所有的边,就是说并不是所有的不等式是满足条件的,那么即使算出可行解,也不能算是满足整个不等式组
步骤:
1,先将xi <= xj + ck 转化为有一条 j - > i 权值为 ck 的边
(xi可能比xj + ck小,或者等于xj + ck,所以是最短路)
2,找一个超级源点,使得该源点一定可以遍历到所有边
3,从源点求一遍单源最短路
结果1,如果出现负环,则原不等式组一定无解
结果2,如果没有负环,则 dist[i] 就是原不等式组的一组可行解
当然,求 最短路 的算法也可以求 最长路 ,
最长路的话就是:
xi >= xj + ck 转化为有一条 j - > i 权值为 ck 的边
xi可能比xj + ck大,或者等于xj + ck,所以是最长路
结果1,如果出现正环,则原不等式组一定无解
结果2,如果没有正环,则 dist[i] 就是原不等式组的一组可行解
求最小值或者最大值
结论:如果求最小值应该求最长路,如果求最大值应该求最短路
为什么求最大值要求最短路呢,
因为如果是求 最大值 ,那么必然有 上界 ,比如 x1 <= 10(如果没有上界的话那就是无限大)
那么比如对于x1来说有以下的几个上界
x1 <= 10
x1 <= 5
x1 <= 8
根据这三个不等式,找出符合条件的且是最大的是 x1 = 5 ,
这就是求最大值要用最短路,那么反过来也一样
问题:如何转化xi <= c,其中c是一个常数,这类的不等式
方法:建立一个超级源点,0,然后建立边 0 - > i,长度为c即可
以求 xi 的 最大值 为例,求所有从 xi 出发,构成的不等式链,xi <= xj + c1 <= xk + c1 + c2 <= … <=c1+c2+c3…所计算出的上界,最终xi的最大值等于所有上界的最小值
接下来就是题目链接
负环 + 差分约束 最短路
第一题
题干第一行说:要求每个小朋友都要分到糖果
那么就说明每一个人的糖果数量至少为1,超级源点 0- > 每一个节点的距离至少为1,这样超级源点满足了可以遍历到所有的边,
老师想知道至少需要准备多少个糖果,所以求的是最小值,那么就是最长路(上面已经解释过),那么建边的时候即为,xi >= xj + ck 转化为有一条 j - > i 权值为 ck 的边
根据输入的五种情况建边,
x = 1,a = b <=> ,a >= b && b >= a
x = 2,a < b <=>, b >= a + 1
x = 3,a >= b <=> ,a >= b
x = 4,a > b <=>,a >= b + 1
x = 5,a <= b <=>,b >= a
按照上面的五种方式和最长路建边规则 ,建边
老师如果不能满足学生的要求,输出 -1,就是说有可能出现 正环 ,用 spfa 判断 正环 ,
因为题目的数据要求是 1e5 个点,1e5个数据边,这样最后累加的数可能会出现越界的情况,那么就用long long,
最多可能会出现多少个边呢,3e5条,所有情况都是x = 1,这样的话就是2e5条,还有每个点和超级源点都有一条长度为1的边,那么就是3e5
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
const int N = 100010, M = 300010;
int n, m;
int h[N], e[M], w[M], ne[M], idx;
int dist[N];
int q[N], cnt[N];
bool st[N];
void add(int a, int b, int c)
{
e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx ++;
}
bool spfa()
{
int hh = 0, tt = 1;
memset(dist, -0x3f, sizeof dist);
dist[0] = 0;
q[0] = 0;
st[0] = true;
while (hh != tt)//如果超时的话就把 队列 换成 栈 因为如果出现该点在负环内部,那么很快就会跳出false
{
int t = q[ -- tt];
st[t] = false;
for (int i = h[t]; ~i; i = ne[i])
{
int j = e[i];
if (dist[j] < dist[t] + w[i])
{
dist[j] = dist[t] + w[i];
cnt[j] = cnt[t] + 1;
if (cnt[j] >= n + 1) return false;
if (!st[j])
{
q[tt ++ ] = j;
st[j] = true;
}
}
}
}
return true;
}
int main()
{
scanf("%d%d", &n, &m);
memset(h, -1, sizeof h);
while (m -- )
{
int x, a, b;
scanf("%d%d%d", &x, &a, &b);
if (x == 1) add(b, a, 0), add(a, b, 0);
else if (x == 2) add(a, b, 1);
else if (x == 3) add(b, a, 0);
else if (x == 4) add(b, a, 1);
else add(a, b, 0);
}
for (int i = 1; i <= n; i ++ ) add(0, i, 1);
if (!spfa()) puts("-1");
else
{
LL res = 0;
for (int i = 1; i <= n; i ++ ) res += dist[i];
printf("%lld\n", res);
}
return 0;
}
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 50010, M = 150010;
int n;
int h[N], e[M], w[M], ne[M], idx;
int dist[N];
int q[N];
bool st[N];
void add(int a, int b, int c)
{
e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx ++ ;
}
void spfa()
{
memset(dist, -0x3f, sizeof dist);
dist[0] = 0;
st[0] = true;
int hh = 0, tt = 1;
q[0] = 0;
while (hh != tt)
{
int t = q[hh ++ ];
if (hh == N) hh = 0;
st[t] = false;
for (int i = h[t]; ~i; i = ne[i])
{
int j = e[i];
if (dist[j] < dist[t] + w[i])
{
dist[j] = dist[t] + w[i];
if (!st[j])
{
q[tt ++ ] = j;
if (tt == N) tt = 0;
st[j] = true;
}
}
}
}
}
int main()
{
scanf("%d", &n);
memset(h, -1, sizeof h);
for (int i = 1; i < N; i ++ )
{
add(i - 1, i, 0);
add(i, i - 1, -1);//任意两相邻值最多只能相差 1
}
for (int i = 0; i < n; i ++ )
{
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
a ++, b ++ ;
add(a - 1, b, c);
}
spfa();
printf("%d\n", dist[50001]);
return 0;
}
差分约束
上来看是
1 号奶牛和 N 号奶牛间可能的最大距离。
求的是最大距离,那么就是最短路,
输出 -1 是说 如果出现 负环,
输出 -2 是说 如果无法找到 节点n 的最小值的话,
麻烦的其实是判断啥时候输出 -2
这两个判断的过程可以分开,也可以合并,这个是分开的,下面代码是合并的,
如果发一个函数调用 两次及以上 的话记得 初始化
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 1001, M = 21010, inf = 0x3f3f3f3f;
int n;
int h[N], ne[M], e[M], w[M], idx;
int dist[N], cnt[N];
int q[N];
bool st[N];
void add(int a,int b, int c){
e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx ++;
}
bool spfa(){
memset(dist, 0x3f, sizeof dist);
int hh = 0 ,tt = 0;
for(int i = 1;i <=n;i ++ ){//全加进去,判断负环
q[tt ++] = i;
st[i] = true;
}
dist[1] = 0;//从 1 到 个节点的单源最短路
while(hh != tt){
int t = q[hh ++];
if(hh == N) hh = 0;
st[t] = false;
for(int i = h[t] ; ~i ; i = ne[i]){
int j = e[i];
if(dist[j] > dist[t] + w[i]){
dist[j] = dist[t] + w[i];
cnt[j] = cnt[t] + 1;
if(cnt[t] >= n)
return false;
if(!st[j]){
q[tt ++] = j;
if(tt == N) tt = 0;
st[j] = true;
}
}
}
}
return true;
}
int main(){
int l, d;
scanf("%d%d%d",&n, &l, &d);
memset(h, -1, sizeof h);
for(int i = 1;i < n;i ++) add(i+ 1, i, 0);
while(l --){
int a, b, c;
scanf("%d%d%d",&a,&b,&c);
if(a > b) swap(a, b);
add(a, b, c);
}
while(d --){
int a, b, c;
scanf("%d%d%d",&a,&b,&c);
if(a > b) swap(a, b);
add(b, a, -c);
}
if(!spfa()) puts("-1");
else{
if(dist[n] == inf)
puts("-2");
else
printf("%d\n",dist[n]);
}
return 0;
}