浅析差分约束

差分约束

前置知识

  • 图的邻接表
  • SPFA(最短路算法)

干嘛用的

对于有 m m m个不等式方程, n n n个变量的方程组
x i − x j < = c k x_i-x_j<=c_k xixj<=ck
. . . . . . . . . . .......... ..........
. . . . . . . . . . .......... ..........
. . . . . . . . . . .......... ..........
1 < = i , j < = n 1<=i,j<=n 1<=i,j<=n

需要求解一组
x 1 , x 2 , . . . . . , x n {x_1, x_2, ....., x_n} x1,x2,.....,xn 满足上面的方程

简单证明

x i − x j < c xi - xj < c xixj<c移项
=> x i < c + x j xi < c + xj xi<c+xj
和图论的松弛操作很像dis[v] > dis[u] + weight(u,v)

也就可以转化为图论

如何选择最短路和最长路

  • 根据题目分析出来
  • 一般求最小值最长路, 最大值最短路
  • 如果构造的不等式为 x i − x y < = c x_i-x_y<=c xixy<=c, 则大多选择最短路
  • 如果构造的不等式为 x i − x y > = c x_i-x_y>=c xixy>=c, 则大多选择最长路

看例题

P5960 最短路类型的模板题

模板题, 直接套模板就行了

//https://www.luogu.com.cn/problem/P5960

#include <queue>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
using ll = long long;
using uint = unsigned int;
const int inf = 0x3f3f3f3f;

const int maxn = 5005;

int cnt = 0;
int p[maxn];

int n, m;
bool inqueue[maxn];
int dis[maxn];
int in[maxn];

struct
{
    int to, w, next;
} edges[maxn];

void addEdge(int from, int to, int weight)
{
    edges[++cnt].to = to;
    edges[cnt].next = p[from];
    edges[cnt].w = weight;
    p[from] = cnt;
}

bool spfa(int s)
{
    memset(dis, inf, sizeof dis);
    memset(in, 0, sizeof in);
    memset(inqueue, false, sizeof inqueue);
    dis[s] = 0;
    queue<int> que;
    //  第一种是把每个点都作为起始点
    for (int i = 1; i <= n; ++i)
    {
        que.push(i);
        inqueue[i] = true;
        in[i]++;
    }
    //****************
    //第二种是添加一层n+1->每个点w为0
    // que.push(s);
    // inqueue[s] = true;
    // in[s]++;
    //************
    while (!que.empty())
    {
        int t = que.front();
        que.pop();
        inqueue[t] = false;
        for (int i = p[t]; i; i = edges[i].next)
        {
            int to = edges[i].to;
            if (dis[to] > dis[t] + edges[i].w)
            {
                dis[to] = dis[t] + edges[i].w;
                if (!inqueue[to])
                {
                    que.push(to);
                    inqueue[to] = true;
                    if (++in[to] > n)
                        return false;
                }
            }
        }
    }
    return true;
}

int main()
{
    int a, b, c;
    scanf("%d%d", &n, &m);
    for (int i = 0; i < m; ++i)
    {
        scanf("%d%d%d", &a, &b, &c);
        addEdge(b, a, c);
    }
    // 第二种
    // for (int i = 0; i <= n; ++i)
    //     addEdge(n + 1, i, 0);
    if (spfa(1))
    {
        for (int i = 1; i <= n; ++i)
            printf("%d%c", dis[i], i < n ? ' ' : '\n');
    }
    else
        printf("NO\n");
    return 0;
}

P3275 最长路类型

求最小值, 当然最长路

//https://www.luogu.com.cn/problem/P3275
#include <queue>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
using ll = long long;
using uint = unsigned int;
const int inf = 0x3f3f3f3f;

const int maxm = 300010; //边的最大数量;
const int maxn = 100005; //节点的最大数量
int cnt = 0;
int p[maxn]; //节点指向的边

int n, m;           //n个节点 m条边
bool inqueue[maxn]; //是否在队列里
int dis[maxn];      //距离
int in[maxn];       //进入队列的次数

struct
{
    int w, to, next;
} edges[maxm]; //edge[0] is None, also we see if it's zero to judge it has been completed

void addEdge(int from, int to, int weight)
{
    edges[++cnt].to = to;
    edges[cnt].next = p[from];
    edges[cnt].w = weight;
    p[from] = cnt;
}

// 如果不存在环的 返回true, 否则返回false
// 求的是s 到每个节点的最短距离
bool spfa(int s)
{
    memset(dis, 0xaf, sizeof dis);
    memset(inqueue, false, sizeof inqueue);
    memset(in, 0, sizeof in);
    queue<int> que;
    que.push(s);
    inqueue[s] = true;
    in[s]++;
    dis[s] = 0;
    while (!que.empty())
    {
        int t = que.front();
        que.pop();
        inqueue[t] = false;
        for (int i = p[t]; i; i = edges[i].next)
        {
            auto to = edges[i].to;
            if (dis[to] < dis[t] + edges[i].w)
            {
                dis[to] = dis[t] + edges[i].w;
                if (!inqueue[to])
                {
                    que.push(to);
                    inqueue[to] = true;
                    if (++in[to] > n)
                        return false;
                }
            }
        }
    }
    return true;
}

int main(int argc, char const *argv[])
{
    int x, a, b;
    scanf("%d%d", &n, &m);
    for (int i = 0; i < m; ++i)
    {
        scanf("%d%d%d", &x, &a, &b);
        if (x == 1)
        {
            addEdge(a, b, 0);
            addEdge(b, a, 0);
        }
        else if (x == 2)
        {
            if (a == b)
            {
                puts("-1");
                return 0;
            }
            addEdge(a, b, 1);
        }
        else if (x == 3)
        {
            addEdge(b, a, 0);
        }
        else if (x == 4)
        {
            if (a == b)
            {
                puts("-1");
                return 0;
            }
            addEdge(b, a, 1);
        }
        else
        {
            addEdge(a, b, 0);
        }
    }
    for (int i = n; i >= 1; --i)
        addEdge(n + 1, i, 1);
    if (spfa(n + 1))
    {
        ll res = 0;
        for (int i = 1; i <= n; ++i)
        {
            res += dis[i];
        }
        printf("%lld\n", res);
    }
    else
    {
        puts("-1");
    }
    return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值