版本比较
BMSSP
正文
增加 45 行删除 45 行
21 行未改动222324252627282930313233343536373839404142434445464748495051525354555657585960616263646 行未改动71727374757677787980818283848586878889909192939495969798991001011021031041059 行未改动1151161171181191201211221231241251261271281299 行未改动13914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818910 行未改动2002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852864 行未改动2912922932942952962972982992 行未改动30230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334421 行未改动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));}// 高性能多源Dijkstravector<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,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,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> 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-basedfor (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)快了不少,具体的我这种蒟蒻也弄不懂。21 行未改动222324252627282930313233343536373839404142434445464748495051525354555657585960616263646 行未改动71727374757677787980818283848586878889909192939495969798991001011021031041059 行未改动1151161171181191201211221231241251261271281299 行未改动13914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818910 行未改动2002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852864 行未改动2912922932942952962972982992 行未改动30230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334421 行未改动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));}// 高性能多源Dijkstravector<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,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,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> 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-basedfor (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)快了不少,具体的我这种蒟蒻也弄不懂。