From c3774ffe41bab82efb3b60364dfbf3451ef3ec08 Mon Sep 17 00:00:00 2001 From: wisdompeak Date: Mon, 2 Jun 2025 00:19:28 -0700 Subject: [PATCH 1/7] Create 3568.Minimum-Moves-to-Clean-the-Classroom.cpp --- ...8.Minimum-Moves-to-Clean-the-Classroom.cpp | 74 +++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 BFS/3568.Minimum-Moves-to-Clean-the-Classroom/3568.Minimum-Moves-to-Clean-the-Classroom.cpp diff --git a/BFS/3568.Minimum-Moves-to-Clean-the-Classroom/3568.Minimum-Moves-to-Clean-the-Classroom.cpp b/BFS/3568.Minimum-Moves-to-Clean-the-Classroom/3568.Minimum-Moves-to-Clean-the-Classroom.cpp new file mode 100644 index 000000000..7a5307a84 --- /dev/null +++ b/BFS/3568.Minimum-Moves-to-Clean-the-Classroom/3568.Minimum-Moves-to-Clean-the-Classroom.cpp @@ -0,0 +1,74 @@ +using state = tuple; +class Solution { +public: + int minMoves(vector& grid, int energy) { + int m = grid.size(), n = grid[0].size(); + + pairstart; + vector>litter_pos; + vector>litter_idx(m, vector(n,-1)); + + for (int i=0; i>>> visited( + m, vector>>( + n, vector>(energy + 1, vector(1 << L, false)))); + + visited[start.first][start.second][energy][0] = true; + + vector>dir={{0,1},{0,-1},{1,0},{-1,0}}; + + queue q; + q.push({start.first, start.second, energy, 0}); + int step = 0; + + while (!q.empty()) + { + int len = q.size(); + while (len--) + { + auto [x,y,e,mask] = q.front(); + q.pop(); + if (mask==(1<=m||ny<0||ny>=n) continue; + + char cell = grid[nx][ny]; + if (cell=='X') continue; + + int newEnergy = e-1; + if (newEnergy < 0) continue; + if (cell == 'R') newEnergy = energy; + + int newMask = mask; + if (grid[nx][ny]=='L') + { + int idx = litter_idx[nx][ny]; + newMask |= (1< Date: Mon, 2 Jun 2025 00:19:54 -0700 Subject: [PATCH 2/7] Update Readme.md --- Readme.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Readme.md b/Readme.md index 2cc7165b0..209f88910 100644 --- a/Readme.md +++ b/Readme.md @@ -621,7 +621,8 @@ [913.Cat-and-Mouse](https://github.com/wisdompeak/LeetCode/tree/master/BFS/913.Cat-and-Mouse) (H+) [1728.Cat-and-Mouse-II](https://github.com/wisdompeak/LeetCode/tree/master/BFS/1728.Cat-and-Mouse-II) (H+) [1293.Shortest-Path-in-a-Grid-with-Obstacles-Elimination](https://github.com/wisdompeak/LeetCode/tree/master/BFS/1293.Shortest-Path-in-a-Grid-with-Obstacles-Elimination) (H-) -[1928.Minimum-Cost-to-Reach-Destination-in-Time](https://github.com/wisdompeak/LeetCode/tree/master/BFS/1928.Minimum-Cost-to-Reach-Destination-in-Time) (H-) +[1928.Minimum-Cost-to-Reach-Destination-in-Time](https://github.com/wisdompeak/LeetCode/tree/master/BFS/1928.Minimum-Cost-to-Reach-Destination-in-Time) (H-) +[3568.Minimum-Moves-to-Clean-the-Classroom](https://github.com/wisdompeak/LeetCode/tree/master/BFS/3568.Minimum-Moves-to-Clean-the-Classroom) (H-) * ``拓扑排序`` [207.Course-Schedule](https://github.com/wisdompeak/LeetCode/tree/master/BFS/207.Course-Schedule) (H-) [210.Course-Schedule-II](https://github.com/wisdompeak/LeetCode/tree/master/BFS/210.Course-Schedule-II) (M) From bc7843350c1b60517252d73e0d33deb6ccec4d6a Mon Sep 17 00:00:00 2001 From: wisdompeak Date: Mon, 2 Jun 2025 00:31:09 -0700 Subject: [PATCH 3/7] Create Readme.md --- BFS/3568.Minimum-Moves-to-Clean-the-Classroom/Readme.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 BFS/3568.Minimum-Moves-to-Clean-the-Classroom/Readme.md diff --git a/BFS/3568.Minimum-Moves-to-Clean-the-Classroom/Readme.md b/BFS/3568.Minimum-Moves-to-Clean-the-Classroom/Readme.md new file mode 100644 index 000000000..a8187eb19 --- /dev/null +++ b/BFS/3568.Minimum-Moves-to-Clean-the-Classroom/Readme.md @@ -0,0 +1,5 @@ +### 3568.Minimum-Moves-to-Clean-the-Classroom + +典型的BFS,但是我们需要定义一个composite state来避免重复。本题中因为最有路径可能需要重复经过相同的cell,所以我们还需要记录每一步时当前的energy,已经访问过的litter。这样所有的状态种类有`20*20*50*2^10=2e7`,复杂度勉强够用。 + +具体的做法就是常规的BFS。每次从队列中弹出[x,y,energy,mask],其中mask是一个二进制数,用bit位来记录哪些编号的垃圾已经被清理。从当前[x,y]往四个方向尝试移动,每次移动需要更新energy(减一,或者遇到R就reset),也可能需要更新mask(即遇到L)。在将新状态加入队列之前,查看一下这个state之前是否已经加入过队列(即可以在更早的时间被访问到)以避免重复搜索。 From cfe262ce8fab231acb34ce3b850c35f6a5565ea7 Mon Sep 17 00:00:00 2001 From: wisdompeak Date: Mon, 2 Jun 2025 01:18:40 -0700 Subject: [PATCH 4/7] Create 3553.Minimum-Weighted-Subgraph-With-the-Required-Paths-II.cpp --- ...ed-Subgraph-With-the-Required-Paths-II.cpp | 80 +++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 Binary_Search/3553.Minimum-Weighted-Subgraph-With-the-Required-Paths-II/3553.Minimum-Weighted-Subgraph-With-the-Required-Paths-II.cpp diff --git a/Binary_Search/3553.Minimum-Weighted-Subgraph-With-the-Required-Paths-II/3553.Minimum-Weighted-Subgraph-With-the-Required-Paths-II.cpp b/Binary_Search/3553.Minimum-Weighted-Subgraph-With-the-Required-Paths-II/3553.Minimum-Weighted-Subgraph-With-the-Required-Paths-II.cpp new file mode 100644 index 000000000..440611fad --- /dev/null +++ b/Binary_Search/3553.Minimum-Weighted-Subgraph-With-the-Required-Paths-II/3553.Minimum-Weighted-Subgraph-With-the-Required-Paths-II.cpp @@ -0,0 +1,80 @@ +using ll = long long; +const int MAXN = 100000; +const int LOGN = 17; +class Solution { +public: + vector> adj[MAXN]; + int up[MAXN][LOGN+1]; + int depth[MAXN]; + ll distRoot[MAXN]; + + void dfs(int cur, int parent) + { + up[cur][0] = parent; + for(auto &[v,w]: adj[cur]) + { + if(v == parent) continue; + depth[v] = depth[cur] + 1; + distRoot[v] = distRoot[cur] + w; + dfs(v, cur); + } + } + + int lca(int a, int b) + { + if(depth[a] < depth[b]) swap(a,b); + int diff = depth[a] - depth[b]; + for(int k = 0; k <= LOGN; k++){ + if(diff & (1<= 0; k--){ + if(up[a][k] != up[b][k]){ + a = up[a][k]; + b = up[b][k]; + } + } + return up[a][0]; + } + + ll dist(int a, int b) + { + int c = lca(a,b); + return distRoot[a] + distRoot[b] - 2*distRoot[c]; + } + + vector minimumWeight(vector>& edges, vector>& queries) + { + int n = edges.size()+1; + + for (int i = 0; i < n-1; i++) + { + int u = edges[i][0], v = edges[i][1], w = edges[i][2]; + adj[u].push_back({v,w}); + adj[v].push_back({u,w}); + } + + depth[0] = 0; + distRoot[0] = 0; + dfs(0, 0); + + for(int k = 1; k <= LOGN; k++) { + for(int v = 0; v < n; v++) { + up[v][k] = up[up[v][k-1]][k-1]; + } + } + + vectorrets; + for (auto& q: queries) + { + int u = q[0], v = q[1], w = q[2]; + ll d_uv = dist(u,v); + ll d_vw = dist(v,w); + ll d_uw = dist(u,w); + ll ans = (d_uv + d_vw + d_uw) / 2; + rets.push_back(ans); + } + + return rets; + } +}; From 0f3873e3c14aa14f736bd5da23fbe7e6c2dc2419 Mon Sep 17 00:00:00 2001 From: wisdompeak Date: Mon, 2 Jun 2025 01:19:19 -0700 Subject: [PATCH 5/7] Update Readme.md --- Readme.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Readme.md b/Readme.md index 209f88910..1106e4e20 100644 --- a/Readme.md +++ b/Readme.md @@ -4,7 +4,7 @@ (If you are interested in joining this group, ping me guan.huifeng@gmail.com) ### LeetCode难题代码和算法要点分析 -#### 目前分类目录 +#### 目前分类目录https://github.com/wisdompeak/LeetCode/tree/master/Binary_Search/3553.Minimum-Weighted-Subgraph-With-the-Required-Paths-II #### [Two Pointers](https://github.com/wisdompeak/LeetCode/tree/master/Two_Pointers) [011.Container-With-Most-Water](https://github.com/wisdompeak/LeetCode/tree/master/Two_Pointers/011.Container-With-Most-Water) (M+) [015.3Sum](https://github.com/wisdompeak/LeetCode/tree/master/Two_Pointers/015.3Sum) (M) @@ -107,6 +107,7 @@ [2836.Maximize-Value-of-Function-in-a-Ball-Passing-Game](https://github.com/wisdompeak/LeetCode/tree/master/Binary_Search/2836.Maximize-Value-of-Function-in-a-Ball-Passing-Game) (H) [2846.Minimum-Edge-Weight-Equilibrium-Queries-in-a-Tree](https://github.com/wisdompeak/LeetCode/tree/master/Binary_Search/2846.Minimum-Edge-Weight-Equilibrium-Queries-in-a-Tree) (H) [2851.String-Transformation](https://github.com/wisdompeak/LeetCode/tree/master/Dynamic_Programming/2851.String-Transformation) (H+) +[3553.Minimum-Weighted-Subgraph-With-the-Required-Paths-II](https://github.com/wisdompeak/LeetCode/tree/master/Binary_Search/3553.Minimum-Weighted-Subgraph-With-the-Required-Paths-II) (H) * ``Binary Search by Value`` [410.Split-Array-Largest-Sum](https://github.com/wisdompeak/LeetCode/tree/master/Dynamic_Programming/410.Split-Array-Largest-Sum) (H-) [774.Minimize-Max-Distance-to-Gas-Station](https://github.com/wisdompeak/LeetCode/tree/master/Priority_Queue/774.Minimize-Max-Distance-to-Gas-Station) (H) From ca0ce462688cdebebacc11d376a48ac77390bec5 Mon Sep 17 00:00:00 2001 From: wisdompeak Date: Mon, 2 Jun 2025 01:19:33 -0700 Subject: [PATCH 6/7] Update Readme.md --- Readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Readme.md b/Readme.md index 1106e4e20..f62e672a9 100644 --- a/Readme.md +++ b/Readme.md @@ -4,7 +4,7 @@ (If you are interested in joining this group, ping me guan.huifeng@gmail.com) ### LeetCode难题代码和算法要点分析 -#### 目前分类目录https://github.com/wisdompeak/LeetCode/tree/master/Binary_Search/3553.Minimum-Weighted-Subgraph-With-the-Required-Paths-II +#### 目前分类目录 #### [Two Pointers](https://github.com/wisdompeak/LeetCode/tree/master/Two_Pointers) [011.Container-With-Most-Water](https://github.com/wisdompeak/LeetCode/tree/master/Two_Pointers/011.Container-With-Most-Water) (M+) [015.3Sum](https://github.com/wisdompeak/LeetCode/tree/master/Two_Pointers/015.3Sum) (M) From ea16c132e87db1f0eab7cc1f60bb155b66331594 Mon Sep 17 00:00:00 2001 From: wisdompeak Date: Mon, 2 Jun 2025 01:23:38 -0700 Subject: [PATCH 7/7] Create Readme.md --- .../Readme.md | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 Binary_Search/3553.Minimum-Weighted-Subgraph-With-the-Required-Paths-II/Readme.md diff --git a/Binary_Search/3553.Minimum-Weighted-Subgraph-With-the-Required-Paths-II/Readme.md b/Binary_Search/3553.Minimum-Weighted-Subgraph-With-the-Required-Paths-II/Readme.md new file mode 100644 index 000000000..21b7d3d06 --- /dev/null +++ b/Binary_Search/3553.Minimum-Weighted-Subgraph-With-the-Required-Paths-II/Readme.md @@ -0,0 +1,3 @@ +### 3553.Minimum-Weighted-Subgraph-With-the-Required-Paths-II + +本题的第一个知识点是:在一棵树里,联通u,v,w三个节点的最小子树的权重和,就是`[dist(u,v)+dist(u,w)+dist(v,w)]/2`.

AltStyle によって変換されたページ (->オリジナル) /