From 625d1ab40ea4c13de815ebb3416a86edd80125ad Mon Sep 17 00:00:00 2001 From: darrenhp Date: Thu, 14 May 2026 00:46:34 +0800 Subject: [PATCH 1/5] fix a bug in bfs01; add multisouce feature; Refactor GridGraph to use std::function for edge cost --- graph/grid_graph_template.hpp | 38 +++++++++++++++++------------------ 1 file changed, 18 insertions(+), 20 deletions(-) diff --git a/graph/grid_graph_template.hpp b/graph/grid_graph_template.hpp index 300e7053..c173b016 100644 --- a/graph/grid_graph_template.hpp +++ b/graph/grid_graph_template.hpp @@ -7,30 +7,30 @@ #include // CUT begin -template struct GridGraph { +template struct GridGraph { const int H, W; const std::vector dx{1, -1, 0, 0}; const std::vector dy{0, 0, 1, -1}; + // T_E (*edge_cost)(int, int, int, int); + function edge_cost; GridGraph() = default; - GridGraph(int h, int w) : H(h), W(w) {} + GridGraph(int h, int w, std::function _edge_cost) : H(h), W(w), edge_cost(move(_edge_cost)) {} // Dijkstra's algorithm // Complexity: O(HWlog(HW)) std::vector> dist; // Distance from (x_s, y_s) std::vector>> prv; // Previous node for Dijkstra optimal path - void dijkstra(int x_s, int y_s) { + void dijkstra(int x_s, int y_s) { dijkstra({x_s, y_s}); } + void dijkstra(std::vector> xy_s) { dist.assign(H, std::vector(W, INF)); prv.assign(H, std::vector>(W, std::make_pair(-1, -1))); using P = std::tuple; std::priority_queue, std::greater

> pq; - dist[x_s][y_s] = 0; - pq.emplace(0, x_s, y_s); + for (auto [x_s, y_s]: xy_s) dist[x_s][y_s] = 0, pq.emplace(0, x_s, y_s); while (!pq.empty()) { - T_E dnow; - int x, y; - std::tie(dnow, x, y) = pq.top(); + auto [dnow, x, y] = pq.top(); pq.pop(); if (dist[x][y] < dnow) continue; for (unsigned d = 0; d < dx.size(); d++) { @@ -46,28 +46,26 @@ template struct Gr } } - void bfs_01(int x_s, int y_s) { + + void bfs_01(int x_s, int y_s) { bfs_01({x_s, y_s}); } + void bfs_01(std::vector> xy_s) { + std::deque> deq; dist.assign(H, std::vector(W, INF)); prv.assign(H, std::vector>(W, std::make_pair(-1, -1))); - std::deque> deq; - dist[x_s][y_s] = 0; - deq.emplace_back(x_s, y_s); + for (auto [x_s, y_s]: xy_s) dist[x_s][y_s] = 0, deq.emplace_back(x_s, y_s); while (!deq.empty()) { - int x, y; - std::tie(x, y) = deq.front(); + auto [x, y] = deq.front(); deq.pop_front(); const auto dnow = dist[x][y]; - deq.pop_front(); for (unsigned d = 0; d < dx.size(); d++) { int xn = x + dx[d], yn = y + dy[d]; if (xn < 0 or yn < 0 or xn >= H or yn >= W) continue; - auto dnxt = dnow + edge_cost(x, y, xn, yn); + auto cost = edge_cost(x, y, xn, yn); + auto dnxt = dnow + cost; if (dnxt < dist[xn][yn]) { dist[xn][yn] = dnxt; prv[xn][yn] = std::make_pair(x, y); - if (dnxt) - deq.emplace_back(xn, yn); - else - deq.emplace_front(xn, yn); + if (cost) deq.emplace_back(xn, yn); + else deq.emplace_front(xn, yn); } } } From 03ed2fc600c05a35b23448d459e11bc22b444893 Mon Sep 17 00:00:00 2001 From: darrenhp Date: Thu, 14 May 2026 10:08:18 +0800 Subject: [PATCH 2/5] format --- graph/grid_graph_template.hpp | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/graph/grid_graph_template.hpp b/graph/grid_graph_template.hpp index c173b016..466b55aa 100644 --- a/graph/grid_graph_template.hpp +++ b/graph/grid_graph_template.hpp @@ -15,7 +15,7 @@ template struct GridGraph { function edge_cost; GridGraph() = default; - GridGraph(int h, int w, std::function _edge_cost) : H(h), W(w), edge_cost(move(_edge_cost)) {} + GridGraph(int h, int w, std::function _edge_cost): H(h), W(w), edge_cost(move(_edge_cost)) {} // Dijkstra's algorithm // Complexity: O(HWlog(HW)) @@ -28,7 +28,10 @@ template struct GridGraph { using P = std::tuple; std::priority_queue, std::greater

> pq; - for (auto [x_s, y_s]: xy_s) dist[x_s][y_s] = 0, pq.emplace(0, x_s, y_s); + for (auto [x_s, y_s]: xy_s) { + dist[x_s][y_s] = 0; + pq.emplace(0, x_s, y_s); + } while (!pq.empty()) { auto [dnow, x, y] = pq.top(); pq.pop(); @@ -52,9 +55,13 @@ template struct GridGraph { std::deque> deq; dist.assign(H, std::vector(W, INF)); prv.assign(H, std::vector>(W, std::make_pair(-1, -1))); - for (auto [x_s, y_s]: xy_s) dist[x_s][y_s] = 0, deq.emplace_back(x_s, y_s); + for (auto [x_s, y_s]: xy_s) { + dist[x_s][y_s] = 0; + deq.emplace_back(x_s, y_s); + } while (!deq.empty()) { - auto [x, y] = deq.front(); deq.pop_front(); + auto [x, y] = deq.front(); + deq.pop_front(); const auto dnow = dist[x][y]; for (unsigned d = 0; d < dx.size(); d++) { int xn = x + dx[d], yn = y + dy[d]; @@ -64,8 +71,11 @@ template struct GridGraph { if (dnxt < dist[xn][yn]) { dist[xn][yn] = dnxt; prv[xn][yn] = std::make_pair(x, y); - if (cost) deq.emplace_back(xn, yn); - else deq.emplace_front(xn, yn); + if (cost > 0) { + deq.emplace_back(xn, yn); + } else { + deq.emplace_front(xn, yn); + } } } } From 215b301605ab100bab595e2cec9aca97a07832c5 Mon Sep 17 00:00:00 2001 From: darrenhp Date: Thu, 14 May 2026 10:17:14 +0800 Subject: [PATCH 3/5] Update grid_graph_template.hpp --- graph/grid_graph_template.hpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/graph/grid_graph_template.hpp b/graph/grid_graph_template.hpp index 466b55aa..13b1f194 100644 --- a/graph/grid_graph_template.hpp +++ b/graph/grid_graph_template.hpp @@ -5,6 +5,7 @@ #include #include #include +#include // CUT begin template struct GridGraph { @@ -12,7 +13,7 @@ template struct GridGraph { const std::vector dx{1, -1, 0, 0}; const std::vector dy{0, 0, 1, -1}; // T_E (*edge_cost)(int, int, int, int); - function edge_cost; + std::function edge_cost; GridGraph() = default; GridGraph(int h, int w, std::function _edge_cost): H(h), W(w), edge_cost(move(_edge_cost)) {} From 43ce1b223d9bf22cd69514cd81026ab0638ed782 Mon Sep 17 00:00:00 2001 From: darrenhp Date: Thu, 14 May 2026 10:21:20 +0800 Subject: [PATCH 4/5] format --- graph/grid_graph_template.hpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/graph/grid_graph_template.hpp b/graph/grid_graph_template.hpp index 13b1f194..54af1c9a 100644 --- a/graph/grid_graph_template.hpp +++ b/graph/grid_graph_template.hpp @@ -16,7 +16,7 @@ template struct GridGraph { std::function edge_cost; GridGraph() = default; - GridGraph(int h, int w, std::function _edge_cost): H(h), W(w), edge_cost(move(_edge_cost)) {} + GridGraph(int h, int w, std::function _edge_cost) : H(h), W(w), edge_cost(move(_edge_cost)) {} // Dijkstra's algorithm // Complexity: O(HWlog(HW)) @@ -29,7 +29,7 @@ template struct GridGraph { using P = std::tuple; std::priority_queue, std::greater

> pq; - for (auto [x_s, y_s]: xy_s) { + for (auto [x_s, y_s] : xy_s) { dist[x_s][y_s] = 0; pq.emplace(0, x_s, y_s); } @@ -56,12 +56,12 @@ template struct GridGraph { std::deque> deq; dist.assign(H, std::vector(W, INF)); prv.assign(H, std::vector>(W, std::make_pair(-1, -1))); - for (auto [x_s, y_s]: xy_s) { + for (auto [x_s, y_s] : xy_s) { dist[x_s][y_s] = 0; deq.emplace_back(x_s, y_s); } while (!deq.empty()) { - auto [x, y] = deq.front(); + auto [x, y] = deq.front(); deq.pop_front(); const auto dnow = dist[x][y]; for (unsigned d = 0; d < dx.size(); d++) { From 3d837d936fd41bb6bdf215229fb05d5105e2b15e Mon Sep 17 00:00:00 2001 From: darrenhp Date: Thu, 14 May 2026 10:25:38 +0800 Subject: [PATCH 5/5] format --- graph/grid_graph_template.hpp | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/graph/grid_graph_template.hpp b/graph/grid_graph_template.hpp index 54af1c9a..66e5f103 100644 --- a/graph/grid_graph_template.hpp +++ b/graph/grid_graph_template.hpp @@ -16,7 +16,8 @@ template struct GridGraph { std::function edge_cost; GridGraph() = default; - GridGraph(int h, int w, std::function _edge_cost) : H(h), W(w), edge_cost(move(_edge_cost)) {} + GridGraph(int h, int w, std::function _edge_cost) + : H(h), W(w), edge_cost(move(_edge_cost)) {} // Dijkstra's algorithm // Complexity: O(HWlog(HW)) @@ -29,10 +30,7 @@ template struct GridGraph { using P = std::tuple; std::priority_queue, std::greater

> pq; - for (auto [x_s, y_s] : xy_s) { - dist[x_s][y_s] = 0; - pq.emplace(0, x_s, y_s); - } + for (auto [x_s, y_s] : xy_s) dist[x_s][y_s] = 0, pq.emplace(0, x_s, y_s); while (!pq.empty()) { auto [dnow, x, y] = pq.top(); pq.pop(); @@ -50,16 +48,12 @@ template struct GridGraph { } } - void bfs_01(int x_s, int y_s) { bfs_01({x_s, y_s}); } void bfs_01(std::vector> xy_s) { std::deque> deq; dist.assign(H, std::vector(W, INF)); prv.assign(H, std::vector>(W, std::make_pair(-1, -1))); - for (auto [x_s, y_s] : xy_s) { - dist[x_s][y_s] = 0; - deq.emplace_back(x_s, y_s); - } + for (auto [x_s, y_s] : xy_s) dist[x_s][y_s] = 0, deq.emplace_back(x_s, y_s); while (!deq.empty()) { auto [x, y] = deq.front(); deq.pop_front();