图(六):差分约束

SPFA–差分约束

差分约束是一种特殊的n元一次不等式组,其中包含n个变量 x 1 , x 2 , . . . . . . , x n x_1, x_2, ...... , x_n x1,x2,......,xn。以及m个约束条件,每个约束条件是由两个其中变量做差构成的,

形如: x i − x j < = c k x_i - x_j <= c_k xixj<=ck ,其中 1 < = i , j < = n , 1 < = k < = m 1<= i, j <= n, 1<= k <= m 1<=i,j<=n,1<=k<=m,并且 c k c_k ck为常数。我们要解决的问题即找到一组可行解 / 最大解/ 最小解,使得所有等式成立,否则无解。

差分约束中的约束条件 x i < = x j + c k x_i <= x_j + c_k xi<=xj+ck 与最短路中的三角不等式 d i s t [ j ] < = d i s t [ t ] + w [ i ] dist[j] <= dist[t] + w[i] dist[j]<=dist[t]+w[i] 非常相似。因此我们可以将所有变量视为一个点,每个约束不等式视为一条有向边, 但是这些约束条件均为相对值,我们可以建立一个虚拟源点 0 号点, 将他初始值设为 0, 向所有点连一条边权为 0 的边。最后采用SPFA算法跑单源最短路,若存在负环则表示不等式组无解,否则dist[i] 即为 x i x_i xi 的解。

小K的农场

很裸的差分约束,处理输入见图跑SPFA即可。

参考代码:

#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cstdio>
#include <queue>
#define int long long
using namespace std;
typedef pair<int, int> PII;
const int N = 1e4 + 10, inf = 1e9;
vector<PII> g[N];
int dist[N], cnt[N];
int n, m;
bool st[N];
bool spfa()
{
    queue<int> q;
    for (int i = 1; i <= n; i++)
    {
        dist[i] = -inf;
    }
    dist[0] = 0;
    st[0] = true;
    q.push(0);
    while (q.size())
    {
        int t = q.front();
        q.pop();
        st[t] = false;
        for (auto tt : g[t])
        {
            int ver = tt.first;
            int dis = tt.second;
            if (dist[ver] < dist[t] + dis)
            {
                dist[ver] = dist[t] + dis;
                if (!st[ver])
                {
                    st[ver] = true;
                    q.push(ver);
                    cnt[ver] = cnt[t] + 1;
                    if (cnt[ver] >= (n + 1))
                    {
                        return false;
                    }
                }
            }
        }
    }
    return true;
}
signed main()
{
    scanf("%lld%lld", &n, &m);
    for (int i = 1; i <= m; i++)
    {
        int x, a, b, c;
        scanf("%lld", &x);
        if (x == 1)
        {
            scanf("%lld%lld%lld", &a, &b, &c);
            g[b].push_back({a, c});
        }
        else if (x == 2)
        {
            scanf("%lld%lld%lld", &a, &b, &c);
            g[a].push_back({b, -c});
        }
        else
        {
            scanf("%lld%lld", &a, &b);
            g[a].push_back({b, 0});
            g[b].push_back({a, 0});
        }
    }
    for (int i = 1; i <= n; i++)
    {
        g[0].push_back({i, 0});
    }
    if (spfa())
    {
        printf("Yes\n");
    }
    else
    {
        printf("No\n");
    }
}

倍杀测量者

**思路:**题目中的 x i x j < = c k \frac{x_i}{x_j} <= c_k xjxi<=ck,仅需两边同时取对数将乘除法转换正加减法即: l o g ( x i ) − l o g ( x j ) < = c k log(x_i) - log(x_j) <= c_k log(xi)log(xj)<=ck,就得到了一个差分约束可以解决的不等式。

参考代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <queue>
using namespace std;
const int N = 1100;
struct flag
{
    int o, a, b, k;
} fl[N];
struct edge
{
    int next, to, tag;
    double w;
} a[N << 1];
int n, s, t, c[N], fr[N], head[N], cnt, vis[N], gt[N];
double dis[N];
queue<int> Q;
void link(int x, int y, int tag, double w)
{
    a[++cnt] = (edge){head[x], y, tag, log2(w)};
    head[x] = cnt;
}
int check(double T)
{
    memset(head, 0, sizeof(head));
    memset(fr, 0, sizeof(fr));
    cnt = 0;
    while (!Q.empty())
        Q.pop();
    for (int i = 1; i <= n; i++)
        if (c[i])
            link(i, 0, 1, c[i]), link(0, i, 0, c[i]);
    for (int i = 1; i <= s; i++)
    {
        int A = fl[i].a, B = fl[i].b, k = fl[i].k, o = fl[i].o;
        if (c[A] && c[B] && ((o == 1 && c[A] < c[B] * (k - T)) || (o == 2 && c[A] * (k + T) < c[B])))
            return 1;
        if (o == 1)
            link(A, B, 1, k - T);
        else
            link(A, B, 0, k + T);
    }
    for (int i = 0; i <= n; i++)
        dis[i] = 0, fr[i] = 0, Q.push(i), vis[i] = 1;
    while (!Q.empty())
    {
        int x = Q.front();
        for (int i = head[x]; i; i = a[i].next)
        {
            int R = a[i].to;
            double w = a[i].tag ? dis[x] - a[i].w : dis[x] + a[i].w;
            if (dis[R] <= w && dis[R] != -1)
                continue;
            dis[R] = w;
            fr[R] = fr[x] + 1;
            if (fr[R] == n + 2)
                return 1;
            if (!vis[R])
                Q.push(R), vis[R] = 1;
        }
        Q.pop();
        vis[x] = 0;
    }
    return 0;
}
int main()
{
    cin >> n >> s >> t;
    double l = 0, r = 1e18, T = -1;
    for (int i = 1; i <= s; i++)
    {
        int o, a, b, k;
        cin >> o >> a >> b >> k;
        fl[i] = (flag){o, a, b, k};
        if (o == 1)
            r = min(r, (float)k - 1e-8);
    }
    for (int i = 1, C, x; i <= t; i++)
        cin >> C >> x, c[C] = x;
    while (r - l > 1e-6)
    {
        double mid = (l + r) / 2.0;
        check(mid) ? l = T = mid : r = mid;
    }
    T == -1 ? puts("-1") : printf("%.10f\n", (double)T);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值