Skip to content

Commit 381598b

Browse files
committed
add: 最小化旅行的价格总和
1 parent 45df77f commit 381598b

File tree

5 files changed

+95
-1
lines changed

5 files changed

+95
-1
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,10 @@ C++标准库提供的数据结构实在是太多了,参考[C++标准库头文
547547

548548
###
549549

550+
- [最小化旅行的价格总和](src/graphs/minimize_the_total_price_of_the_trips.cpp) [树, 深度优先搜索, 图, 数组, 动态规划]
551+
552+
- LeetCode 2646. 最小化旅行的价格总和 <https://leetcode.cn/problems/minimize-the-total-price-of-the-trips>
553+
550554
- [到达首都的最少油耗](src/graphs/minimum_fuel_cost_to_report_to_the_capital.cpp) [树, 深度优先搜索, 广度优先搜索, 图]
551555

552556
- LeetCode 2477. 到达首都的最少油耗 <https://leetcode.cn/problems/minimum-fuel-cost-to-report-to-the-capital>
Loading
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// 最小化旅行的价格总和
2+
// https://leetcode.cn/problems/minimize-the-total-price-of-the-trips
3+
// INLINE ../../images/graphs/minimize_the_total_price_of_the_trips.jpeg
4+
// 参考官方题解,dfs + dp
5+
6+
#include <headers.hpp>
7+
8+
class Solution {
9+
public:
10+
bool dfs(int node, int parent, int end, vector<vector<int>> &next,
11+
vector<int> &count) {
12+
if (node == end) {
13+
count[node]++;
14+
return true;
15+
}
16+
for (int child : next[node]) {
17+
if (child == parent) {
18+
continue;
19+
}
20+
if (dfs(child, node, end, next, count)) {
21+
count[node]++;
22+
return true;
23+
}
24+
}
25+
return false;
26+
}
27+
28+
pair<int, int> dp(int node, int parent, vector<vector<int>> &next,
29+
vector<int> &price, vector<int> &count) {
30+
pair<int, int> res = {price[node] * count[node],
31+
price[node] * count[node] / 2};
32+
for (int child : next[node]) {
33+
if (child == parent) {
34+
continue;
35+
}
36+
auto [x, y] = dp(child, node, next, price, count);
37+
res.first += min(x, y); // node 没有减半,因此可以取子树的两种情况的最小值
38+
res.second += x; // node 减半,只能取子树没有减半的情况
39+
}
40+
return res;
41+
}
42+
43+
int minimumTotalPrice(int n, vector<vector<int>> &edges, vector<int> &price,
44+
vector<vector<int>> &trips) {
45+
vector<vector<int>> graph(n);
46+
vector<vector<int>> next(n);
47+
for (auto &edge : edges) {
48+
next[edge[0]].push_back(edge[1]);
49+
next[edge[1]].push_back(edge[0]);
50+
}
51+
52+
vector<int> count(n);
53+
for (auto &trip : trips) {
54+
dfs(trip[0], -1, trip[1], next, count);
55+
}
56+
57+
auto [x, y] = dp(0, -1, next, price, count);
58+
return min(x, y);
59+
}
60+
};
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include <graphs/minimize_the_total_price_of_the_trips.cpp>
2+
3+
TEST(最小化旅行的价格总和, minimumTotalPrice) {
4+
Solution solution;
5+
// 示例 1:
6+
// 输入:n = 4, edges = [[0,1],[1,2],[1,3]], price = [2,2,10,6], trips =
7+
// [[0,3],[2,1],[2,3]] 输出:23 解释: 上图表示将节点 2
8+
// 视为根之后的树结构。第一个图表示初始树,第二个图表示选择节点 0 、2 和 3
9+
// 并使其价格减半后的树。 第 1 次旅行,选择路径 [0,1,3] 。路径的价格总和为 1 +
10+
// 2 + 3 = 6 。 第 2 次旅行,选择路径 [2,1] 。路径的价格总和为 2 + 5 = 7 。 第
11+
// 3 次旅行,选择路径 [2,1,3] 。路径的价格总和为 5 + 2 + 3 = 10 。
12+
// 所有旅行的价格总和为 6 + 7 + 10 = 23 。可以证明,23 是可以实现的最小答案。
13+
vector<vector<int>> edges1 = {{0, 1}, {1, 2}, {1, 3}};
14+
vector<int> price1 = {2, 2, 10, 6};
15+
vector<vector<int>> trips1 = {{0, 3}, {2, 1}, {2, 3}};
16+
EXPECT_EQ(solution.minimumTotalPrice(4, edges1, price1, trips1), 23);
17+
18+
// 示例 2:
19+
// 输入:n = 2, edges = [[0,1]], price = [2,2], trips = [[0,0]]
20+
// 输出:1
21+
// 解释:
22+
// 上图表示将节点 0
23+
// 视为根之后的树结构。第一个图表示初始树,第二个图表示选择节点 0
24+
// 并使其价格减半后的树。 第 1 次旅行,选择路径 [0] 。路径的价格总和为 1 。
25+
// 所有旅行的价格总和为 1 。可以证明,1 是可以实现的最小答案。
26+
vector<vector<int>> edges2 = {{0, 1}};
27+
vector<int> price2 = {2, 2};
28+
vector<vector<int>> trips2 = {{0, 0}};
29+
EXPECT_EQ(solution.minimumTotalPrice(2, edges2, price2, trips2), 1);
30+
}

test/lib/lib_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// 执行编译时间:2023-12-05 10:36:39
1+
// 执行编译时间:2023-12-06 09:50:10
22
#include <gtest/gtest.h>
33
#include <lib.hpp>
44

0 commit comments

Comments
 (0)