BMSSP算法有多快
最近清华的大佬研究出来了比Dijkstra快的算法。
话不多说,下面是模板代码:
#include <iostream>
#include <vector>
#include <queue>
#include <cmath>
#include <unordered_set>
#include <algorithm>
#include <cstdlib>
#include <ctime>
#include <climits>
using namespace std;
typedef long long ll;
const ll INF = (1LL << 60); // 足够大的不可达值
class FastGraph {
int n; // 节点数
int m; // 边数
vector<vector<pair<int, ll> > > adj; // 邻接表
// 子树分析数据结构
struct SubtreeInfo {
int depth;
int size;
SubtreeInfo() : depth(0), size(0) {}
};
public:
FastGraph(int nodes, int edges) : n(nodes), m(edges), adj(nodes) {}
void addEdge(int u, int v, ll w) {
adj[u].push_back(make_pair(v, w));
}
// 高性能多源Dijkstra
vector<ll> multiDijkstra(const unordered_set<int>& sources) {
vector<ll> dist(n, INF);
vector<bool> visited(n, false);
priority_queue<pair<ll, int>, vector<pair<ll, int> >, greater<pair<ll, int> > > pq;
for (unordered_set<int>::const_iterator it = sources.begin(); it != sources.end(); ++it) {
int u = *it;
dist[u] = 0;
pq.push(make_pair(0, u));
}
while (!pq.empty()) {
ll d = pq.top().first;
int u = pq.top().second;
pq.pop();
if (visited[u]) continue;
visited[u] = true;
for (size_t j = 0; j < adj[u].size(); j++) {
int v = adj[u][j].first;
ll w = adj[u][j].second;
ll newDist = d + w;
if (newDist < dist[v]) {
dist[v] = newDist;
pq.push(make_pair(newDist, v));
}
}
}
return dist;
}
// 精确子树分析
void analyzeSubtrees(const vector<ll>& dist,
const vector<int>& parent,
vector<SubtreeInfo>& info) {
vector<vector<int> > children(n);
for (int i = 0; i < n; i++) {
if (parent[i] != -1) {
children[parent[i]].push_back(i);
}
}
vector<int> order;
vector<int> stack;
for (int i = 0; i < n; i++) {
if (parent[i] == -1 && dist[i] < INF) {
stack.push_back(i);
}
}
while (!stack.empty()) {
int u = stack.back();
stack.pop_back();
order.push_back(u);
for (size_t j = 0; j < children[u].size(); j++) {
stack.push_back(children[u][j]);
}
}
reverse(order.begin(), order.end());
for (size_t i = 0; i < order.size(); i++) {
int u = order[i];
info[u].size = 1;
for (size_t j = 0; j < children[u].size(); j++) {
int v = children[u][j];
info[u].size += info[v].size;
info[u].depth = max(info[u].depth, info[v].depth + 1);
}
}
}
// 关键节点筛选算法
vector<int> findPivots(const unordered_set<int>& S, ll B, int k) {
vector<ll> dist(n, INF);
vector<int> parent(n, -1);
vector<int> frontier;
for (unordered_set<int>::const_iterator it = S.begin(); it != S.end(); ++it) {
int u = *it;
dist[u] = 0;
frontier.push_back(u);
}
for (int step = 0; step < k && !frontier.empty(); ++step) {
vector<int> nextFrontier;
for (size_t i = 0; i < frontier.size(); i++) {
int u = frontier[i];
for (size_t j = 0; j < adj[u].size(); j++) {
int v = adj[u][j].first;
ll w = adj[u][j].second;
ll newDist = dist[u] + w;
if (newDist < min(dist[v], B)) {
dist[v] = newDist;
parent[v] = u;
nextFrontier.push_back(v);
}
}
}
frontier.swap(nextFrontier);
}
vector<int> expandedSet;
for (int i = 0; i < n; i++) {
if (dist[i] < B) {
expandedSet.push_back(i);
}
}
if (expandedSet.empty() || (int)expandedSet.size() > k * (int)S.size()) {
return vector<int>(S.begin(), S.end());
}
vector<SubtreeInfo> subtreeInfo(n);
analyzeSubtrees(dist, parent, subtreeInfo);
vector<int> pivots;
for (size_t i = 0; i < expandedSet.size(); i++) {
int u = expandedSet[i];
if (subtreeInfo[u].size >= k && subtreeInfo[u].depth >= (int)ceil(log2(k))) {
pivots.push_back(u);
}
}
if (pivots.empty() || (int)pivots.size() < (int)expandedSet.size() / (2*k)) {
return vector<int>(S.begin(), S.end());
}
return pivots;
}
// 前沿缩减策略
void reduceFrontier(unordered_set<int>& S, ll B, int k,
vector<ll>& globalDist) {
vector<ll> dist(n, INF);
vector<int> parent(n, -1);
vector<int> activeSet;
for (unordered_set<int>::const_iterator it = S.begin(); it != S.end(); ++it) {
int u = *it;
dist[u] = globalDist[u];
activeSet.push_back(u);
}
for (int step = 0; step < k; step++) {
vector<int> nextActiveSet;
for (size_t i = 0; i < activeSet.size(); i++) {
int u = activeSet[i];
for (size_t j = 0; j < adj[u].size(); j++) {
int v = adj[u][j].first;
ll w = adj[u][j].second;
ll newDist = dist[u] + w;
if (newDist < min(dist[v], B)) {
dist[v] = newDist;
parent[v] = u;
nextActiveSet.push_back(v);
}
}
}
if (nextActiveSet.empty()) break;
activeSet.swap(nextActiveSet);
}
vector<SubtreeInfo> subtreeInfo(n);
analyzeSubtrees(dist, parent, subtreeInfo);
unordered_set<int> finished;
for (int i = 0; i < n; i++) {
if (dist[i] < B && subtreeInfo[i].depth < k) {
finished.insert(i);
globalDist[i] = min(globalDist[i], dist[i]);
}
}
unordered_set<int> newS;
for (unordered_set<int>::const_iterator it = S.begin(); it != S.end(); ++it) {
int u = *it;
if (!finished.count(u) && dist[u] < B) {
newS.insert(u);
}
}
vector<int> pivots = findPivots(newS, B, k);
for (size_t i = 0; i < pivots.size(); i++) {
newS.insert(pivots[i]);
}
S.swap(newS);
}
// 分层递归主函数
vector<ll> BMSSP(int l, ll B, unordered_set<int> S,
vector<ll> globalDist = vector<ll>()) {
if (globalDist.empty()) {
globalDist.resize(n, INF);
for (unordered_set<int>::const_iterator it = S.begin(); it != S.end(); ++it) {
int u = *it;
globalDist[u] = 0;
}
}
int k = max(2, static_cast<int>(pow(n, 1.0/(l+1))));
k = min(k, 1000);
if (l > 0 && S.size() > 1) {
reduceFrontier(S, B, k, globalDist);
}
if (l == 0 || (int)S.size() <= k) {
vector<ll> localDist = multiDijkstra(S);
for (int i = 0; i < n; i++) {
if (localDist[i] < globalDist[i]) {
globalDist[i] = localDist[i];
}
}
return globalDist;
}
vector<int> pivots = findPivots(S, B, k);
ll newB = B;
for (size_t pi = 0; pi < pivots.size(); pi++) {
int pivot = pivots[pi];
unordered_set<int> subRegion;
subRegion.insert(pivot);
vector<ll> subDist = BMSSP(l-1, newB, subRegion, globalDist);
for (int i = 0; i < n; i++) {
if (subDist[i] < globalDist[i]) {
globalDist[i] = subDist[i];
newB = min(newB, globalDist[i]);
}
}
vector<int> updateQueue;
for (int i = 0; i < n; i++) {
if (globalDist[i] < INF) {
updateQueue.push_back(i);
}
}
for (size_t ui = 0; ui < updateQueue.size(); ui++) {
int u = updateQueue[ui];
for (size_t j = 0; j < adj[u].size(); j++) {
int v = adj[u][j].first;
ll w = adj[u][j].second;
ll newDist = globalDist[u] + w;
if (newDist < globalDist[v]) {
globalDist[v] = newDist;
}
}
}
}
return globalDist;
}
// 算法入口
vector<ll> shortestPath(int src) {
int depth = 1;
if (n > 100) {
depth = max(1, static_cast<int>(pow(log2(n), 2.0/3.0)));
depth = min(depth, 8);
}
ll initBound = INF;
unordered_set<int> initSet;
initSet.insert(src);
return BMSSP(depth, initBound, initSet);
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, m, s;
cin >> n >> m >> s;
FastGraph g(n, m);
for (int i = 0; i < m; i++) {
int u, v;
ll w;
cin >> u >> v >> w;
g.addEdge(u-1, v-1, w); // 转换为0-based索引
}
vector<ll> dist = g.shortestPath(s-1); // 起点转换为0-based
for (int i = 0; i < n; i++) {
if (dist[i] >= INF) {
cout << "2147483647\n"; // 不可达时输出题目常见的大数
} else {
cout << dist[i] << ' ';
}
}
return 0;
}额,上边代码是Deepsek提供的,然后我们提交以下P4779就会发现:BMSSP相比于Dijkstra堆优化快了不少,具体的我这种蒟蒻也弄不懂。