stl - 用 C++ STL 实现 Dijkstra 算法
问题描述
我已经实现了 Dijkstra 算法如下
#include <iostream>
#include <bits/stdc++.h>
#include<cstdio>
#define ll long long int
#define mod 1000000007
#define pi 3.141592653589793
#define f first
#define s second
#define pb push_back
#define pf push_front
#define pob pop_back
#define pof pop_front
#define vfor(e, a) for (vector<ll> :: iterator e = a.begin(); e != a.end(); e++)
#define vfind(a, e) find(a.begin(), a.end(), e)
#define forr(i, n) for (ll i = 0; i < n; i++)
#define rfor(i, n) for (ll i = n - 1; i >= 0; i--)
#define fors(i, b, e, steps) for(ll i = b; i < e; i += steps)
#define rfors(i, e, b, steps) for(ll i = e; i > b; i -= steps)
#define mp make_pair
using namespace std;
void up(pair<ll, ll> a[], ll n, ll i, ll indArray[]) {
ll ind = (i - 1) / 2;
while (ind >= 0 && a[ind].s > a[i].s) {
swap(a[ind], a[i]);
indArray[a[ind].f] = ind;
indArray[a[i].f] = i;
i = ind;
ind = (i - 1) / 2;
}
}
void down(pair<ll, ll> a[], ll n, ll i, ll indArray[]) {
ll left = 2 * i + 1;
ll right = 2 * i + 2;
ll m = a[i].s;
ll ind = i;
if (left < n && a[left].s < m) {
ind = left;
m = a[left].s;
}
if (right < n && a[right].s < m) {
ind = right;
}
if (ind != i) {
swap(a[i], a[ind]);
indArray[a[i].f] = i;
indArray[a[ind].f] = ind;
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
// cout << setprecision(10);
ll n, m;
cin >> n >> m;
vector<pair<ll, ll>> a[n];
forr(i, m) {
ll u, v, w;
cin >> u >> v >> w;
a[u].pb(mp(v, w));
a[v].pb(mp(u, w));
}
ll parent[n];
parent[0] = -1;
pair<ll, ll> dist[n];
forr(i, n) {
dist[i] = mp(i, INT_MAX);
}
dist[0].s = 0;
ll ind[n];
iota(ind, ind + n, 0);
ll ans[n];
ans[0] = 0;
bool visited[n];
fill(visited, visited + n, false);
ll size = n;
forr(i, n) {
ll u = dist[0].f;
visited[u] = true;
ll d1 = dist[0].s;
ans[u] = dist[0].s;
swap(dist[0], dist[size - 1]);
size--;
down(dist, size, 0, ind);
for (auto e : a[u]) {
if (visited[e.f]){
continue;
}
ll v = e.f;
ll j = ind[v];
if (dist[j].s > d1 + e.s) {
dist[j].s = d1 + e.s;
up(dist, size, j, ind);
parent[v] = u;
}
}
}
stack<ll> st;
forr(i, n) {
ll j = i;
while (j != -1) {
st.push(j);
j = parent[j];
}
while (!st.empty()) {
cout << st.top() << "->";
st.pop();
}
cout << " Path length is " << ans[i];
cout << '\n';
}
}
这个实现是正确的并且给出了正确的输出。
正如每次我选择具有最低键值(与源的距离)的节点一样,然后我更新所选节点的所有相邻节点上的键。更新相邻节点的键后,我调用“up”函数来维护最小堆属性。但优先级队列存在于 c++ stl 中。我如何使用它们来避免上下功能。
问题是我需要能够在需要更新其密钥的平均堆中找到节点密钥对的索引。在这段代码中,我使用了一个单独的 ind 数组,每次更新最小堆时都会更新该数组。但是如何利用 c++ stl
解决方案
就像你暗示的那样,我们不能有效地随机访问std::priority_queue
. 对于这种情况,我建议您使用std::set
. 它实际上不是堆,而是平衡的二叉搜索树。但是,它以您想要的方式工作。find
,insert
和erase
方法都是O(log n)
这样你可以插入/擦除/更新所需时间的值,因为更新可以通过擦除然后插入来完成。访问最小值是O(1)
.
您可以像我提到的那样参考这个参考实现。对于您的邻接列表,时间复杂度是O(E log V)
其中 E 是边数,V 是顶点数。
请注意
- 使用默认比较器,
std::set::begin()
如果非空,方法返回最小元素 - 在这段代码中,它将距离作为第一,索引作为第二。通过这样做,集合元素按距离升序排序
% 我没有详细研究你的代码的up
实现down
。
推荐阅读
- python-3.x - DBSCAN 返回 TypeError:无效的类型提升
- angular - forkJoin 内部的角度单元测试
- jupyter-notebook - ipywidgets Widget - 日期选择器 - Mozilla Firefox 的问题
- mysql - 从一组表数据生成/设计一个 json 对象
- reactjs - Botpress - 自定义组件访问会话变量
- firebase - 构建 React Native Firebase 应用程序时 MyApplication.java 中的错误
- laravel - 使用门面时获得 403 Forbidden
- python - 为什么 urllib.parse 不拆分 URL
: 在所有情况下都正确吗? - java - 在子类中使用泛型时发生奇怪的编译错误
- ssl - Kong SSL 卸载以将反向代理从 https 设置为 http