OJSF 信息学 Wiki · 第 69 / 144 节 · K061

69 · K061 Dijkstra 与堆优化

独立过关 · 样例与错因 · 课末题

100 分钟。用反例区分“第一次发现”“第一次以最小且有效的标签弹出”。用非负权证明后者,而不是把“有权图”一律交给 Dijkstra。

1. 先修自查

从 1 出发,1→2 权 10,1→3 权 2,3→2 权 1。第一次看到 2 的候选是 10,真正最短为 3。这里不是每条边代价一样的 BFS;若一入堆就把 2 永久标记,会错。

2. 暂定距离和已确定距离

最小堆保存 (候选距离,点)。每次取全局最小候选;若距离不是当前 dist[u],那是旧条目,跳过。其余条目能用于松弛:若 dist[u]+w<dist[v],更新 v 并推新条目。无需修改堆里旧条目。路径正好相等时不用再入堆;严格小于可避免零权环不断重复压堆。

为什么有效最小条目可确定?假设到 u 有更短路,沿这条路从源出发找第一个未确定的点 x。它前面的点已被确定并松弛过 x。非负边保证这段前缀不长于整条更短路径,所以 x 应该比 u 更早出堆,矛盾。负边会破坏这一步:1→2(2)、1→3(5)、3→2(−10),点 2 若在弹出时直接定案就会得 2,真正答案是 −5。

3. 跟做:旧堆项不是新答案

输入 4 5 1,边为 1 2 10、1 3 2、3 2 1、2 4 0、3 4 6。第 7 区程序输出 0 3 2 3。

弹出更新记忆点
(0,1)dist[2]=10、dist[3]=2这里只是入堆
(2,3)dist[2]=3、dist[4]=8旧 10 不必从堆中删除
(3,2)dist[4]=3权 0 合法
(3,4)无有效标签
(8,4)、(10,2)跳过已被更小标签替代

4. 改一条规则:同价时选边少的路线

路线代价从单个距离变为二元组 (总距离,边数),按字典序比较。例如 (4,1) 优于 (4,3) 但不优于 (3,8)。走一条边的增量为 (w,1);w≥0 时非负(甚至 w=0 也会增加边数),因此同样能按最小有效标签确定。不能只用距离判断旧条目:同距离但边数更多的条目照样过期。

5. 独立过关:距离相同就少换站

第一行 n m s t,随后 m 行有向边 u v w;1≤n≤100000,0≤m≤200000,0≤w≤10^9。若不可达输出 NONE,否则同一行输出最短距离与其中最少边数(边是实际行驶段,不减 1)。样例 4 4 1 4,边 1 2 0、2 3 0、3 4 4、1 4 4,输出 4 1。再测 s=t,输出 0 0。

6. 提示、程序与错因

展开:统一比较 (距离,边数)

用 vector 邻接表,堆键由 distance、hops、编号依次比较;是否过期只看前两项。时间 O(n+(m+1)log(m+2))(同一点可入堆多次),空间 O(n+m)。复测:让早入堆的路径距离相同但边更多,程序仍应取更少边数。

#include <functional>
#include <iostream>
#include <queue>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;
struct Edge { int v,w; };
int main() {
    ios::sync_with_stdio(false); cin.tie(nullptr);
    int n,m,s,t; cin >> n >> m >> s >> t;
    vector<vector<Edge>> g(n+1);
    while (m--) { int u,v,w; cin >> u >> v >> w; g[u].push_back({v,w}); }
    using Label=pair<long long,int>;
    using Item=tuple<long long,int,int>;
    const long long INF=1LL<<60;
    vector<Label> best(n+1,{INF,0});
    priority_queue<Item,vector<Item>,greater<Item>> q;
    best[s]={0,0}; q.emplace(0,0,s);
    while (!q.empty()) {
        long long d; int h,u;
        tie(d,h,u)=q.top(); q.pop();
        if (Label(d,h)!=best[u]) continue; // 两个坐标都对才是当前标签
        if (u==t) break; // 不能在第一次把 t 放进堆时结束
        for (const Edge &e:g[u]) {
            Label cand={d+e.w,h+1};
            if (cand<best[e.v]) {
                best[e.v]=cand; q.emplace(cand.first,cand.second,e.v);
            }
        }
    }
    if (best[t].first==INF) cout << "NONE\n";
    else cout << best[t].first << ' ' << best[t].second << '\n';
}

7. 经典题解析:洛谷 P4779 单源最短路(标准版)

n≤10^5、m≤2×10^5、非负权,保证起点到各点可达。矩阵既浪费空间又会把找候选和枚举出边变成大量无用扫描。用邻接表存真实有向边,用小根堆维护最小候选,每次有效弹出才处理出边。第 3 区手算说明重入堆和过期判断是正常机制。每条边至多引发一次成功松弛,时间 O(n+(m+1)log(m+2)),空间 O(n+m);输入大时使用快速流。此题不需要输出不可达占位符,若换成别的题要重新读输出要求。

#include <functional>
#include <iostream>
#include <queue>
#include <utility>
#include <vector>
using namespace std;
struct Edge { int v,w; };
int main() {
    ios::sync_with_stdio(false); cin.tie(nullptr);
    int n,m,s; cin >> n >> m >> s;
    vector<vector<Edge>> g(n+1);
    while (m--) {
        int u,v,w; cin >> u >> v >> w; g[u].push_back({v,w}); // 只存题意方向
    }
    const long long INF=1LL<<60;
    vector<long long> dist(n+1,INF);
    using Item=pair<long long,int>;
    priority_queue<Item,vector<Item>,greater<Item>> pq;
    dist[s]=0; pq.push({0,s});
    while (!pq.empty()) {
        Item top=pq.top(); pq.pop();
        long long d=top.first;
        int u=top.second;
        if (d!=dist[u]) continue; // 丢掉已经被更小标签替换的旧项
        for (const Edge &e:g[u]) if (d+e.w<dist[e.v]) {
            dist[e.v]=d+e.w; pq.push({dist[e.v],e.v}); // 更短才更新
        }
    }
    for (int v=1;v<=n;++v) {
        if (v>1) cout << ' ';
        cout << dist[v];
    }
    cout << '\n';
}

上一节:Floyd与多源最短路 · 回到课程目录