HDU3592差分约束

Posted hesorchen

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU3592差分约束相关的知识,希望对你有一定的参考价值。

题目

World Exhibition

思路

按照题目建图即可。

需要注意的是,用最短路求解差分约束系统,是把某一个变量(即图中的源点)假设为一个值X,以此来推出其他变量可取的最大值。同理,用最长路求解差分约束系统,是把某一个变量(即图中的源点)假设为一个值X,以此来推出其他变量可取的最小值。

这题要求1到n的最大距离,只需以1为起点跑最短路求出 D i s n Dis_n Disn最大值或者以n为起点跑最长路求出 D i s 1 Dis_1 Dis1最小值。

代码

最短路

#include <bits/stdc++.h>
using namespace std;

const long long N = 1e6 + 5;
const long long INF = 1e18;
struct node
{
    long long v, w, next;
} edge[3 * N];
long long head[N];
long long dis[N];
long long cnt[N];
long long vis[N];
long long ct = 1;
void add(long long u, long long v, long long w)
{
    edge[ct].v = v;
    edge[ct].w = w;
    edge[ct].next = head[u];
    head[u] = ct++;
}
long long n, x, y;

bool spfa(long long beg)
{
    queue<long long> q;
    q.push(beg);
    vis[beg] = 1;
    dis[beg] = 0;
    while (q.size())
    {
        long long u = q.front();
        q.pop();
        vis[u] = 0;
        for (long long i = head[u]; i; i = edge[i].next)
        {
            long long v = edge[i].v;
            long long w = edge[i].w;
            if (dis[u] + w < dis[v])
            {
                dis[v] = dis[u] + w;
                if (!vis[v])
                {
                    q.push(v);
                    if (++cnt[v] > n)
                        return false;
                    vis[v] = 1;
                }
            }
        }
    }
    return true;
}
int main()
{ /*
    用Di表示第i个人位于坐标Di上
    对于条件x:a b c
    有Db-Da<=c
    对于条件y:a b c
    有Db-Da>=c
    要求的是Dn-D0的最大值 用最短路
    */
    long long t;
    scanf("%lld", &t);
    while (t--)
    {
        scanf("%lld %lld %lld", &n, &x, &y);
        ct = 1;
        for (long long i = 0; i <= n; i++)
            cnt[i] = head[i] = vis[i] = 0, dis[i] = INF;
        for (long long i = 1; i <= x; i++)
        {
            long long a, b, c;
            scanf("%lld %lld %lld", &a, &b, &c);
            if (a > b)
                swap(a, b);
            add(a, b, c);
        }
        for (long long i = 1; i <= y; i++)
        {
            long long a, b, c;
            scanf("%lld %lld %lld", &a, &b, &c);
            if (a > b)
                swap(a, b);
            add(b, a, -c);
        }
        for (long long i = 2; i <= n; i++) //Di-D(i-1)>=0
            add(i, i - 1, 0);

        if (!spfa(1))
            cout << -1 << endl;
        else if (dis[n] == INF)
            cout << -2 << endl;
        else
            cout << dis[n] - dis[1] << endl;
    }
    return 0;
}

最长路

#include <bits/stdc++.h>
using namespace std;

const long long N = 1e6 + 5;
const long long INF = 1e18;
struct node
{
    long long v, w, next;
} edge[3 * N];
long long head[N];
long long dis[N];
long long cnt[N];
long long vis[N];
long long ct = 1;
void add(long long u, long long v, long long w)
{
    edge[ct].v = v;
    edge[ct].w = w;
    edge[ct].next = head[u];
    head[u] = ct++;
}
long long n, x, y;

bool spfa(long long beg)
{
    queue<long long> q;
    q.push(beg);
    vis[beg] = 1;
    dis[beg] = 0;
    while (q.size())
    {
        long long u = q.front();
        q.pop();
        vis[u] = 0;
        for (long long i = head[u]; i; i = edge[i].next)
        {
            long long v = edge[i].v;
            long long w = edge[i].w;
            if (dis[u] + w > dis[v])
            {
                dis[v] = dis[u] + w;
                if (!vis[v])
                {
                    q.push(v);
                    if (++cnt[v] > n)
                        return false;
                    vis[v] = 1;
                }
            }
        }
    }
    return true;
}
int main()
{ /*
    用Di表示第i个人位于坐标Di上
    对于条件x:a b c
    有Db-Da<=c
    对于条件y:a b c
    有Db-Da>=c
    要求的是Dn-D0的最大值 用最短路
    */
    long long t;
    scanf("%lld", &t);
    while (t--)
    {
        scanf("%lld %lld %lld", &n, &x, &y);
        ct = 1;
        for (long long i = 0; i <= n; i++)
            cnt[i] = head[i] = vis[i] = 0, dis[i] = -INF;
        for (long long i = 1; i <= x; i++)
        {
            long long a, b, c;
            scanf("%lld %lld %lld", &a, &b, &c);
            if (a > b)
                swap(a, b);
            add(b, a, -c);
        }
        for (long long i = 1; i <= y; i++)
        {
            long long a, b, c;
            scanf("%lld %lld %lld", &a, &b, &c);
            if (a > b)
                swap(a, b);
            add(a, b, c);
        }
        for (long long i = 2; i <= n; i++) //Di-D(i-1)>=0
            add(i - 1, i, 0);
        if (!spfa(n))
            cout << -1 << endl;
        else if (dis[1] == -INF)
            cout << -2 << endl;
        else
            cout << dis[n] - dis[1] << endl;
    }
    return 0;
}

以上是关于HDU3592差分约束的主要内容,如果未能解决你的问题,请参考以下文章

hdu3592(差分约束) (线性)

HDU3592取log转化为差分约束系统

HDU 3592 查分约束+判环

HDU1534差分约束

HDU1531 差分约束

HDU3666 差分约束