跳到正文

算法·理论

7.15差分约束测试题解

hhsyh_0929约 13 分钟
原文
文章信息与版本

文章信息

发布
2026年7月15日
收录
2026年7月15日
更新
2026年11月16日
篇幅
约 13 分钟

版本

  1. v1当前

7.15差分约束测试题解

OpenJ_Bailian - 1716 Integer Intervals

思路:

1.用前缀和存储每个区间,即S[b]-S[a-1]>=2,转成下界约束:S[a-1]<=S[b]-2;

2.每个整数只占一个元素,所以隐含约束:0<=S[i+1]-s[i]<=1,即S[i]<=S[i+1]和S[i]<=S[i-1]-1;

代码:

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

const int N = 5005;
const int INF = 1e9;

struct Edge {
    int v, w;
};

int n, m;
vector<Edge> g[N];
int dis[N], cnt[N];
bool inq[N];

bool spfa(int s) {
    queue<int> q;
    for (int i = 0; i <= n; i++) {
        dis[i] = INF;
        inq[i] = false;
        cnt[i] = 0;
    }
    dis[s] = 0;
    q.push(s);
    inq[s] = true;

    while (!q.empty()) {
        int u = q.front();
        q.pop();
        inq[u] = false;

        for (auto e : g[u]) {
            int v = e.v, w = e.w;
            if (dis[v] > dis[u] + w) {
                dis[v] = dis[u] + w;
                if (!inq[v]) {
                    inq[v] = true;
                    q.push(v);
                    if (++cnt[v] > n + 1) return false; 
                }
            }
        }
    }
    return true;
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin >> n >> m;

    for (int i = 1; i <= m; i++) {
        int type, a, b, c;
        cin >> type;
        if (type == 1) {
            cin >> a >> b >> c;
            g[a].push_back({b, -c});
        } else if (type == 2) {
            cin >> a >> b >> c;
            g[b].push_back({a, c});
        } else {
            cin >> 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(0)) {
        cout << "Yes\n";
    } else {
        cout << "No\n";
    }
    return 0;
}

P1993 小 K 的农场

思路:

1.将题目中给出的条件转化为差分约束的松弛条件

x[a]-x[b]>=c->x[b]<=-c+x[a]

x[a]-x[b]<=c->x[a]<=c+x[b]

x[a]=x[b]->x[a]<=x[b]+0 x[b]<=x[a]+0

代码:

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

const int N = 5005;
const int INF = 1e9;

struct Edge {
    int v, w;
};

int n, m;
vector<Edge> g[N];
int dis[N], cnt[N];
bool inq[N];

bool spfa(int s) {
    queue<int> q;
    for (int i = 0; i <= n; i++) {
        dis[i] = INF;
        inq[i] = false;
        cnt[i] = 0;
    }
    dis[s] = 0;
    q.push(s);
    inq[s] = true;

    while (!q.empty()) {
        int u = q.front();
        q.pop();
        inq[u] = false;

        for (auto e : g[u]) {
            int v = e.v, w = e.w;
            if (dis[v] > dis[u] + w) {
                dis[v] = dis[u] + w;
                if (!inq[v]) {
                    inq[v] = true;
                    q.push(v);
                    if (++cnt[v] > n + 1) return false; 
                }
            }
        }
    }
    return true;
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin >> n >> m;

    for (int i = 1; i <= m; i++) {
        int type, a, b, c;
        cin >> type;
        if (type == 1) {
            cin >> a >> b >> c;
            g[a].push_back({b, -c});
        } else if (type == 2) {
            cin >> a >> b >> c;
            g[b].push_back({a, c});
        } else {
            cin >> 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(0)) {
        cout << "Yes\n";
    } else {
        cout << "No\n";
    }
    return 0;
}

P2294 [HNOI2005] 狡猾的商人

思路:

1.对于m次的信息,整合成前缀和S[t]-S[s-1]=v;

2.转化为约束条件:s[t]<=S[s-1]+v和S[s-1]<=s[t]+w;

3.直接建边:s-1->t,边权v,t->s-1,边权-v;

代码:

C++
#include<bits/stdc++.h>
#define int long long
#define pii pair<int,int>
using namespace std;

const int N = 3e4 + 5;

int n, m;
int dis[N], cnt[N];
bool vis[N];
vector<pii> g[N];

bool spfa(int s) {
    memset(dis, 0x3f, sizeof(dis));
    memset(vis, 0, sizeof(vis));
    memset(cnt, 0, sizeof(cnt));
    queue<int> q;
    dis[s] = 0;
    vis[s] = 1;
    cnt[s] = 1;
    q.push(s);

    while (!q.empty()) {
        int u = q.front();
        q.pop();
        vis[u] = 0;
        for (auto e : g[u]) {
            int v = e.first, w = e.second;
            if (dis[v] > dis[u] + w) {
                dis[v] = dis[u] + w;
                if (!vis[v]) {
                    vis[v] = 1;
                    cnt[v]++;
                    if (cnt[v] > n + 1) return true; // 存在负环
                    q.push(v);
                }
            }
        }
    }
    return false;
}

signed main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    int T;
    cin >> T;
    while (T--) {
        cin >> n >> m;
        for (int i = 0; i <= n + 1; i++) g[i].clear();

        for (int i = 1; i <= m; i++) {
            int s, t, v;
            cin >> s >> t >> v;
            g[t].push_back({s - 1, -v});
            g[s - 1].push_back({t, v});
        }

        for (int i = 0; i <= n; i++) {
            g[n + 1].push_back({i, 0});
        }

        if (spfa(n + 1)) {
            cout << "false\n";
        } else {
            cout << "true\n";
        }
    }
    return 0;
}

OpenJ_Bailian - 3424 Candies

思路:

1.令x[i]为编号i的孩子得到的糖果数:x[b]-x[a]<=C->x[b]<=x[a]+C;

2.所有边权C为非负数,为提升时间复杂度,使用 Dijkstra算法求解最短路,SPFA 会 TLE。

代码:

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

const int N = 30005;
const int INF = 1e9;

struct Edge {
    int v, w;
};

int n, m;
vector<Edge> g[N];
int dist[N];
bool vis[N];

struct Node {
    int v, d;
    bool operator<(const Node &other) const {
        return d > other.d; // 小根堆
    }
};

void dijkstra(int s) {
    for (int i = 1; i <= n; i++) {
        dist[i] = INF;
        vis[i] = false;
    }
    priority_queue<Node> pq;
    dist[s] = 0;
    pq.push({s, 0});

    while (!pq.empty()) {
        Node cur = pq.top();
        pq.pop();
        int u = cur.v;
        if (vis[u]) continue;
        vis[u] = true;

        for (auto &e : g[u]) {
            int v = e.v, w = e.w;
            if (dist[v] > dist[u] + w) {
                dist[v] = dist[u] + w;
                pq.push({v, dist[v]});
            }
        }
    }
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);

    cin >> n >> m;
    for (int i = 1; i <= m; i++) {
        int u, v, w;
        cin >> u >> v >> w;
        g[u].push_back({v, w});
    }

    dijkstra(1);
    cout << dist[n] << '\n';
    return 0;
}
原文