Skip to content

Commit

Permalink
add floyd path-counter; add dynamic floyd; add dynamic spfa;
Browse files Browse the repository at this point in the history
  • Loading branch information
old-yan committed Nov 4, 2024
1 parent 991a690 commit 03375b0
Show file tree
Hide file tree
Showing 55 changed files with 2,441 additions and 310 deletions.
9 changes: 5 additions & 4 deletions DS/AdjDiff.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@
4. [P1117 [NOI2016] 优秀的拆分](https://www.luogu.com.cn/problem/P1117)
5. [P2367 语文成绩](https://www.luogu.com.cn/problem/P2367)
6. [P4655 [CEOI2017] Building Bridges](https://www.luogu.com.cn/problem/P4655)
7. [P10843 【MX-J2-T4】Turtle and Cycles](https://www.luogu.com.cn/problem/P10843)
8. [fsl 的背包](https://ac.nowcoder.com/acm/problem/263978)
9. [k - 路径(hard vension)](https://ac.nowcoder.com/acm/problem/279411)
10. [Static Range Sum](https://judge.yosupo.jp/problem/static_range_sum)(https://github.com/yosupo06/library-checker-problems/issues/398)
7. [P7516 [省选联考 2021 A/B 卷] 图函数](https://www.luogu.com.cn/problem/P7516)
8. [P10843 【MX-J2-T4】Turtle and Cycles](https://www.luogu.com.cn/problem/P10843)
9. [fsl 的背包](https://ac.nowcoder.com/acm/problem/263978)
10. [k - 路径(hard vension)](https://ac.nowcoder.com/acm/problem/279411)
11. [Static Range Sum](https://judge.yosupo.jp/problem/static_range_sum)(https://github.com/yosupo06/library-checker-problems/issues/398)


### 二、模板功能
Expand Down
7 changes: 4 additions & 3 deletions DS/GlobalUnorderedDiscretizer.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@

​ 练习题目:

1. [#2774. 「BalticOI 2018」多角恋](https://loj.ac/p/2774)
2. [P1955 [NOI2015] 程序自动分析](https://www.luogu.com.cn/problem/P1955)
3. [U428478 祖孙询问](https://www.luogu.com.cn/problem/U428478)
1. [Arbitrage](https://acm.hdu.edu.cn/showproblem.php?pid=1217)
2. [#2774. 「BalticOI 2018」多角恋](https://loj.ac/p/2774)
3. [P1955 [NOI2015] 程序自动分析](https://www.luogu.com.cn/problem/P1955)
4. [U428478 祖孙询问](https://www.luogu.com.cn/problem/U428478)

### 二、模板功能

Expand Down
1 change: 1 addition & 0 deletions DS/LinearDSU.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

1. [1488. 避免洪水泛滥](https://leetcode.cn/problems/avoid-flood-in-the-city/)
2. [P4145 上帝造题的七分钟 2 / 花神游历各国](https://www.luogu.com.cn/problem/P4145)
3. [小苯的蓄水池(hard)](https://ac.nowcoder.com/acm/problem/281338)


### 二、模板功能
Expand Down
45 changes: 23 additions & 22 deletions DS/LinkBucket.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,29 @@

​ 练习题目:

1. [Question for the Leader](https://acm.hdu.edu.cn/showproblem.php?pid=5329)
2. [Hotaru's problem](https://acm.hdu.edu.cn/showproblem.php?pid=5371)
3. [P3246 [HNOI2016] 序列](https://www.luogu.com.cn/problem/P3246)
4. [P3261 [JLOI2015] 城池攻占](https://www.luogu.com.cn/problem/P3261)
5. [P3402 可持久化并查集](https://www.luogu.com.cn/problem/P3402)
6. [P3835 【模板】可持久化平衡树](https://www.luogu.com.cn/problem/P3835)
7. [P3919 【模板】可持久化线段树 1(可持久化数组)](https://www.luogu.com.cn/problem/P3919)
8. [P4062 [Code+#1] Yazid 的新生舞会](https://www.luogu.com.cn/problem/P4062)
9. [P4151 [WC2011] 最大XOR和路径](https://www.luogu.com.cn/problem/P4151)
10. [P4211 [LNOI2014] LCA](https://www.luogu.com.cn/problem/P4211)
11. [P4556 [Vani有约会] 雨天的尾巴 /【模板】线段树合并](https://www.luogu.com.cn/problem/P4556)
12. [P5022 [NOIP2018 提高组] 旅行](https://www.luogu.com.cn/problem/P5022)
13. [P5049 [NOIP2018 提高组] 旅行 加强版](https://www.luogu.com.cn/problem/P5049)
14. [P5287 [HNOI2019] JOJO](https://www.luogu.com.cn/problem/P5287)
15. [P6822 [PA2012] Tax](https://www.luogu.com.cn/problem/P6822)
16. [U208135 可持久化并查集 加强版](https://www.luogu.com.cn/problem/U208135)
17. [记忆](https://ac.nowcoder.com/acm/problem/274793)
18. [parent 树上启发式合并](https://ac.nowcoder.com/acm/problem/274852)
19. [Persistent Queue](https://judge.yosupo.jp/problem/persistent_queue)(https://github.com/yosupo06/library-checker-problems/issues/379)
20. [Persistent Unionfind](https://judge.yosupo.jp/problem/persistent_unionfind)(https://github.com/yosupo06/library-checker-problems/issues/405)
21. [Cycle Detection (Directed)](https://judge.yosupo.jp/problem/cycle_detection)(https://github.com/yosupo06/library-checker-problems/issues/534)
22. [Cycle Detection (Undirected)](https://judge.yosupo.jp/problem/cycle_detection_undirected)(https://github.com/yosupo06/library-checker-problems/issues/869)
1. [find the safest road](https://acm.hdu.edu.cn/showproblem.php?pid=1596)
2. [Question for the Leader](https://acm.hdu.edu.cn/showproblem.php?pid=5329)
3. [Hotaru's problem](https://acm.hdu.edu.cn/showproblem.php?pid=5371)
4. [P3246 [HNOI2016] 序列](https://www.luogu.com.cn/problem/P3246)
5. [P3261 [JLOI2015] 城池攻占](https://www.luogu.com.cn/problem/P3261)
6. [P3402 可持久化并查集](https://www.luogu.com.cn/problem/P3402)
7. [P3835 【模板】可持久化平衡树](https://www.luogu.com.cn/problem/P3835)
8. [P3919 【模板】可持久化线段树 1(可持久化数组)](https://www.luogu.com.cn/problem/P3919)
9. [P4062 [Code+#1] Yazid 的新生舞会](https://www.luogu.com.cn/problem/P4062)
10. [P4151 [WC2011] 最大XOR和路径](https://www.luogu.com.cn/problem/P4151)
11. [P4211 [LNOI2014] LCA](https://www.luogu.com.cn/problem/P4211)
12. [P4556 [Vani有约会] 雨天的尾巴 /【模板】线段树合并](https://www.luogu.com.cn/problem/P4556)
13. [P5022 [NOIP2018 提高组] 旅行](https://www.luogu.com.cn/problem/P5022)
14. [P5049 [NOIP2018 提高组] 旅行 加强版](https://www.luogu.com.cn/problem/P5049)
15. [P5287 [HNOI2019] JOJO](https://www.luogu.com.cn/problem/P5287)
16. [P6822 [PA2012] Tax](https://www.luogu.com.cn/problem/P6822)
17. [U208135 可持久化并查集 加强版](https://www.luogu.com.cn/problem/U208135)
18. [记忆](https://ac.nowcoder.com/acm/problem/274793)
19. [parent 树上启发式合并](https://ac.nowcoder.com/acm/problem/274852)
20. [Persistent Queue](https://judge.yosupo.jp/problem/persistent_queue)(https://github.com/yosupo06/library-checker-problems/issues/379)
21. [Persistent Unionfind](https://judge.yosupo.jp/problem/persistent_unionfind)(https://github.com/yosupo06/library-checker-problems/issues/405)
22. [Cycle Detection (Directed)](https://judge.yosupo.jp/problem/cycle_detection)(https://github.com/yosupo06/library-checker-problems/issues/534)
23. [Cycle Detection (Undirected)](https://judge.yosupo.jp/problem/cycle_detection_undirected)(https://github.com/yosupo06/library-checker-problems/issues/869)



Expand Down
2 changes: 1 addition & 1 deletion GRAPH/BFS.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ namespace OY {
struct DistanceNode<void, false> {
size_type m_val;
};
template <typename CountType, bool GetPath>
template <typename CountType = void, bool GetPath = false>
struct Solver {
using node = DistanceNode<CountType, GetPath>;
static constexpr bool has_count = !std::is_void<CountType>::value;
Expand Down
9 changes: 5 additions & 4 deletions GRAPH/BFS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@

​ 练习题目:

1. [LCP 21. 追逐游戏](https://leetcode.cn/problems/Za25hA/)
2. [P1144 最短路计数](https://www.luogu.com.cn/problem/P1144)
3. [P1443 马的遍历](https://www.luogu.com.cn/problem/P1443)
4. [P7293 [USACO21JAN] Sum of Distances P](https://www.luogu.com.cn/problem/P7293)
1. [Frogger](https://acm.hdu.edu.cn/showproblem.php?pid=1918)
2. [LCP 21. 追逐游戏](https://leetcode.cn/problems/Za25hA/)
3. [P1144 最短路计数](https://www.luogu.com.cn/problem/P1144)
4. [P1443 马的遍历](https://www.luogu.com.cn/problem/P1443)
5. [P7293 [USACO21JAN] Sum of Distances P](https://www.luogu.com.cn/problem/P7293)


### 二、模板功能
Expand Down
64 changes: 39 additions & 25 deletions GRAPH/BellmanFord.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ msvc14.2,C++14
#include <cstdint>
#include <limits>
#include <numeric>
#include <type_traits>
#include <vector>

namespace OY {
Expand All @@ -38,31 +39,44 @@ namespace OY {
struct DistanceNode<Tp, void, false> {
Tp m_val;
};
template <typename ValueType, typename SumType = ValueType, SumType Inf = std::numeric_limits<SumType>::max() / 2>
struct AddSemiGroup {
template <typename Tp, bool IsNumeric = std::is_integral<Tp>::value || std::is_floating_point<Tp>::value>
struct SafeInfinite {
static constexpr Tp max() { return std::numeric_limits<Tp>::max() / 2; }
};
template <typename Tp>
struct SafeInfinite<Tp, false> {
static constexpr Tp max() { return std::numeric_limits<Tp>::max(); }
};
template <typename ValueType, typename SumType = ValueType, typename Compare = std::less<SumType>, SumType Inf = SafeInfinite<SumType>::max()>
struct AddGroup {
using value_type = ValueType;
using sum_type = SumType;
using compare_type = Compare;
static sum_type op(const sum_type &x, const value_type &y) { return x + y; }
static sum_type identity() { return Inf; }
static sum_type identity() { return {}; }
static sum_type infinite() { return Inf; }
};
template <typename ValueType, ValueType Inf = std::numeric_limits<ValueType>::max() / 2>
struct MaxSemiGroup {
template <typename ValueType, typename Compare = std::less<ValueType>, ValueType Inf = SafeInfinite<ValueType>::max()>
struct MaxGroup {
using value_type = ValueType;
using sum_type = ValueType;
using compare_type = Compare;
static sum_type op(const sum_type &x, const sum_type &y) { return std::max(x, y); }
static sum_type identity() { return Inf; }
static sum_type identity() { return {}; }
static sum_type infinite() { return Inf; }
};
template <typename SemiGroup, typename CountType, typename Compare = std::less<typename SemiGroup::sum_type>, bool GetPath = false>
template <typename Group, typename CountType = void, bool GetPath = false>
struct Solver {
using group = SemiGroup;
using group = Group;
using value_type = typename group::value_type;
using sum_type = typename group::sum_type;
using compare_type = typename group::compare_type;
using node = DistanceNode<sum_type, CountType, GetPath>;
static constexpr bool has_count = !std::is_void<CountType>::value;
using count_type = typename std::conditional<has_count, CountType, bool>::type;
size_type m_vertex_cnt;
std::vector<node> m_distance;
static sum_type infinite() { return group::identity(); }
static sum_type infinite() { return group::infinite(); }
Solver(size_type vertex_cnt) : m_vertex_cnt(vertex_cnt), m_distance(vertex_cnt) {
for (size_type i = 0; i != m_vertex_cnt; i++) {
m_distance[i].m_val = infinite();
Expand All @@ -85,22 +99,22 @@ namespace OY {
}
for (size_type i = 0; i != m_vertex_cnt && last == i - 1; i++)
traverser([&](size_type index, size_type from, size_type to, const value_type &dis) {
if (Compare()(m_distance[from].m_val, infinite())) {
if (compare_type()(m_distance[from].m_val, infinite())) {
sum_type to_dis = group::op(m_distance[from].m_val, dis);
if constexpr (has_count) {
if (Compare()(to_dis, m_distance[to].m_val)) {
if (compare_type()(to_dis, m_distance[to].m_val)) {
last = i, m_distance[to].m_val = to_dis, m_distance[to].m_cnt = m_distance[from].m_cnt;
if constexpr (GetPath) m_distance[to].m_from = index;
recs[index] = {m_distance[from].m_val, m_distance[from].m_cnt};
} else if (!Compare()(m_distance[to].m_val, to_dis))
if (Compare()(m_distance[from].m_val, recs[index].m_val)) {
} else if (!compare_type()(m_distance[to].m_val, to_dis))
if (compare_type()(m_distance[from].m_val, recs[index].m_val)) {
last = i, m_distance[to].m_cnt += m_distance[from].m_cnt;
recs[index] = {m_distance[from].m_val, m_distance[from].m_cnt};
} else if (m_distance[from].m_cnt != recs[index].m_cnt) {
last = i, m_distance[to].m_cnt += m_distance[from].m_cnt - recs[index].m_cnt;
recs[index].m_cnt = m_distance[from].m_cnt;
}
} else if (Compare()(to_dis, m_distance[to].m_val)) {
} else if (compare_type()(to_dis, m_distance[to].m_val)) {
last = i, m_distance[to].m_val = to_dis;
if constexpr (GetPath) m_distance[to].m_from = index;
}
Expand All @@ -121,7 +135,7 @@ namespace OY {
if constexpr (has_count)
return m_distance[target].m_cnt;
else
return Compare()(m_distance[target].m_val, infinite());
return compare_type()(m_distance[target].m_val, infinite());
}
};
template <typename Tp>
Expand All @@ -142,24 +156,24 @@ namespace OY {
m_edges.clear(), m_edges.reserve(edge_cnt);
}
void add_edge(size_type a, size_type b, Tp dis) { m_edges.push_back({a, b, dis}); }
template <typename SemiGroup = AddSemiGroup<Tp, Tp, std::numeric_limits<Tp>::max() / 2>, typename CountType = void, typename Compare = std::less<typename SemiGroup::sum_type>, bool GetPath = false>
std::pair<Solver<SemiGroup, CountType, Compare, GetPath>, bool> calc(size_type source) const {
auto res = std::make_pair(Solver<SemiGroup, CountType, Compare, GetPath>(m_vertex_cnt), false);
res.first.set_distance(source, 0);
template <typename Group = AddGroup<Tp>, typename CountType = void, bool GetPath = false>
std::pair<Solver<Group, CountType, GetPath>, bool> calc(size_type source) const {
auto res = std::make_pair(Solver<Group, CountType, GetPath>(m_vertex_cnt), false);
res.first.set_distance(source, Group::identity());
res.second = res.first.run(*this);
return res;
}
template <typename SemiGroup = AddSemiGroup<Tp, Tp, std::numeric_limits<Tp>::max() / 2>, typename Compare = std::less<typename SemiGroup::sum_type>>
template <typename Group = AddGroup<Tp>>
bool has_negative_cycle(size_type source) const {
Solver<SemiGroup, void, Compare, false> sol(m_vertex_cnt);
sol.set_distance(source, 0);
Solver<Group> sol(m_vertex_cnt);
sol.set_distance(source, Group::identity());
return !sol.run(*this);
}
template <typename SemiGroup = AddSemiGroup<Tp, Tp, std::numeric_limits<Tp>::max() / 2>, typename Compare = std::less<typename SemiGroup::sum_type>>
template <typename Group = AddGroup<Tp>>
std::vector<size_type> get_path(size_type source, size_type target) const {
std::vector<size_type> res;
Solver<SemiGroup, void, Compare, true> sol(m_vertex_cnt);
sol.set_distance(source, 0);
Solver<Group, void, true> sol(m_vertex_cnt);
sol.set_distance(source, Group::identity());
if (!sol.run(*this)) return res;
res.push_back(source);
auto find = [&](size_type index) { return m_edges[index].m_from; };
Expand Down
Loading

0 comments on commit 03375b0

Please sign in to comment.