题解:P6168 [IOI 2016] railroad

· · 题解

P6168 题解

前言

:::info[AI 使用说明]

作者使用 DeepSeek 进行了:帮助我自己理解题目,一定程度上的调试,以及指出本篇题解的缺点。

其中,个别语句采用了 DeepSeek 的建议叙述方式,但题解主体完全由作者自主完成。

:::

题意简述

给定 n 个路段,每个路段在进入时限速 s_i,在离开时将速度设为 t_i。在两个路段之间,你可以使用 1 的代价使速度 -1。

最开始速度为 1,求一种经过所有路段的方式,使得代价最小。

题目分析

题目中说“限速 s_i”,即速度 \le s_i,不容易处理,考虑等效将其转化为 = s_i:

题目中说“经过每个路段恰好一次”,表面上是求哈密尔顿路。但哈密尔顿路复杂度过高,考虑如何将这个问题转化为欧拉路径问题:

我们先在原图上做一些预处理:

以下称 m 条路段边和 \operatorname{INF} \rarr 1 这条虚拟路段边构成的图为原图。

至此,将原问题转化为欧拉回路问题还是存在如下两个困难:

鉴于连通性考虑,我们尝试在图上增加一些边,形成新图,目标是使新图上的一条欧拉回路对应题目所求的一条合法路径。

需要注意的是,我们并不需要真正使用欧拉回路算法求解一条欧拉回路,而是在增加边的过程中,把新增边的边权加入到代价中,从而求解最小代价。

在加边之前,我们除了需要知道有效节点,还需要预处理出原图的出入度数和连通性情况(使用并查集维护)。

考虑如何加边,才能同时满足正确性和最优性:

这里还有一个关键点:满足度数限制应该在保证连通性之前。顺序颠倒的话,正确性仍然可以保证,但最优性就会被破坏。因为满足度数条件的过程中可以“顺便”连接一些节点,这么做可以减小后面通过 \operatorname{MST} 保证连通性部分所花的代价。故先满足度数限制可以做到更优。

通过几种加边的方式,原问题已经完全转化为一个欧拉回路问题,总代价为:满足度数限制所加边的代价 + 通过 \operatorname{MST} 保证连通性的代价。

至此,原问题已经得到解决,复杂度 O(n \log n),可以接受。

:::warning[警示后人]

:::

代码实现

:::success[代码]

#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <set>
#include <map>
#include <tuple>
using namespace std;

using LL = long long;
using Edge = tuple<LL, LL, LL>;

struct UnionFindSet {
    vector<LL> p;

    UnionFindSet(LL n) {
        for (LL i = 0; i <= n; i++) {
            p.push_back(i);
        }
    }

    LL find(LL u) {
        if (p[u] == u) {
            return u;
        }
        return (p[u] = find(p[u]));
    }

    void join(LL u, LL v) {
        LL root_u = find(u);
        LL root_v = find(v);
        if (root_u > root_v) {
            swap(root_u, root_v);
        }
        p[root_v] = root_u;
    }

    bool connected(LL u, LL v) {
        return (find(u) == find(v));
    }
};

const LL INF = 1e9 + 10;
const LL N = 4e5 + 10;
LL s[N], t[N], d[N], r_flow[N];

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

    LL m, _;
    cin >> m >> _;
    set<LL> node_set;
    node_set.insert(1);
    node_set.insert(INF);
    for (LL i = 1; i <= m; i++) {
        cin >> s[i] >> t[i];
        node_set.insert(s[i]);
        node_set.insert(t[i]);
    }
    map<LL, LL> id_of;
    LL n = node_set.size();
    vector<LL> nodes(1, 0);
    for (LL u : node_set) {
        nodes.push_back(u);
        id_of[u] = nodes.size() - 1;
    }
    s[++m] = INF; t[m] = 1;
    memset(d, 0, sizeof(d));
    UnionFindSet ufs(n);
    for (LL i = 1; i <= m; i++) {
        LL u = id_of[s[i]];
        LL v = id_of[t[i]];
        d[u]++; d[v]--;
        ufs.join(u, v);
    }
    r_flow[0] = 0;
    for (LL i = 1; i <= n; i++) {
        r_flow[i] = r_flow[i - 1] + d[i];
    }
    LL cost_sum = 0;
    for (LL i = 1; i < n; i++) {
        if (r_flow[i] == 0) {
            continue;
        }
        if (r_flow[i] > 0) {
            cost_sum += r_flow[i] * (nodes[i + 1] - nodes[i]);
        }
        ufs.join(i, i + 1);
    }
    vector<Edge> edges;
    for (LL i = 1; i < n; i++) {
        LL w = nodes[i + 1] - nodes[i];
        edges.push_back({w, i, i + 1});
    }
    sort(edges.begin(), edges.end());
    for (Edge edge : edges) {
        LL u, v, w;
        tie(w, u, v) = edge;
        if (ufs.connected(u, v)) {
            continue;
        }
        ufs.join(u, v);
        cost_sum += w;
    }
    cout << cost_sum << endl;
    return 0;
}

:::