版本比较
BMSSP
正文
增加 45 行删除 45 行
21 行未改动22232425262627282930313233333435363637383940414243444546464748495051525253545556575758596060616263646 行未改动71727374757576777879808182838484858687888990919292939495969798991001011011021031041059 行未改动1151161171181191191201211221231241251251261271281299 行未改动13914014114214314314414514614714814915015015115215315415415515615715715815916016116216316416516516616716816916917017117217317417417517617717817917918018118218318418518518618718818910 行未改动2002012022032042042052062072072082092102112122132142152152162172182192202212222232232242252262272282282292302312322332332342352362372382392402412422422432442452452462472482492492502512522532542552562572582592592602612612622632642652662672682682692702712722732742752752762772782792802812822822832842852864 行未改动2912922932942952952962972982992 行未改动30230330430530630630730830931031031131231331431531631731831831932032132132232332332432532632732832933033033133233233333433533633733833934034034134234334421 行未改动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;6 行未改动 return dist; }
// 精确子树分析 void analyzeSubtrees(const vector<ll>& 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;9 行未改动 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];9 行未改动 } } 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, 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];10 行未改动 } 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> 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;4 行未改动 } } } }
return globalDist; }
// 算法入口2 行未改动 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](https://www.luogu.com.cn/problem/P4779)就会发现:[BMSSP](https://www.luogu.com.cn/record/231752895)相比于[Dijkstra堆优化](https://www.luogu.com.cn/record/231752653)快了不少,具体的我这种蒟蒻也弄不懂。