Skip to content

Commit 45df77f

Browse files
committed
add: 到达首都的最少油耗
1 parent 2e0936e commit 45df77f

File tree

5 files changed

+84
-1
lines changed

5 files changed

+84
-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/minimum_fuel_cost_to_report_to_the_capital.cpp) [树, 深度优先搜索, 广度优先搜索, 图]
551+
552+
- LeetCode 2477. 到达首都的最少油耗 <https://leetcode.cn/problems/minimum-fuel-cost-to-report-to-the-capital>
553+
550554
- [T 秒后青蛙的位置](src/graphs/frog_position_after_t_seconds.cpp) [树, 深度优先搜索, 广度优先搜索, 图]
551555

552556
- LeetCode 1377. T 秒后青蛙的位置 <https://leetcode.cn/problems/frog-position-after-t-seconds/>
Loading
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// 到达首都的最少油耗
2+
// https://leetcode.cn/problems/minimum-fuel-cost-to-report-to-the-capital
3+
// INLINE ../../images/graphs/minimum_fuel_cost_to_report_to_the_capital.jpeg
4+
5+
#include <headers.hpp>
6+
7+
class Solution {
8+
public:
9+
long long int minimumFuelCost(vector<vector<int>> &roads, int seats) {
10+
int n = roads.size() + 1;
11+
vector<vector<int>> graph(n);
12+
for (auto &road : roads) {
13+
int src = road[0], dest = road[1];
14+
graph[src].push_back(dest);
15+
graph[dest].push_back(src);
16+
}
17+
18+
long long int totalCost = 0;
19+
traverse(0, -1, graph, totalCost, seats);
20+
return totalCost;
21+
}
22+
23+
private:
24+
int traverse(int node, int parent, vector<vector<int>> &graph,
25+
long long int &cost, int seats) {
26+
int total = 1;
27+
for (int neighbour : graph[node]) {
28+
if (neighbour != parent) {
29+
int sub_total = traverse(neighbour, node, graph, cost, seats);
30+
cost += (long long int)ceil(
31+
(double)sub_total /
32+
seats); // casting to double for correct division and then
33+
// converting back to long long int
34+
total += sub_total;
35+
}
36+
}
37+
return total;
38+
}
39+
};
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#include <graphs/minimum_fuel_cost_to_report_to_the_capital.cpp>
2+
3+
TEST(到达首都的最少油耗, minimumFuelCost) {
4+
Solution solution;
5+
// 示例 1:
6+
// 输入:roads = [[0,1],[0,2],[0,3]], seats = 5
7+
// 输出:3
8+
// 解释:
9+
// - 代表 1 直接到达首都,消耗 1 升汽油。
10+
// - 代表 2 直接到达首都,消耗 1 升汽油。
11+
// - 代表 3 直接到达首都,消耗 1 升汽油。
12+
// 最少消耗 3 升汽油。
13+
vector<vector<int>> roads1 = {{0, 1}, {0, 2}, {0, 3}};
14+
int seats1 = 5;
15+
EXPECT_EQ(solution.minimumFuelCost(roads1, seats1), 3);
16+
17+
// 示例 2:
18+
// 输入:roads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2
19+
// 输出:7
20+
// 解释:
21+
// - 代表 2 到达城市 3 ,消耗 1 升汽油。
22+
// - 代表 2 和代表 3 一起到达城市 1 ,消耗 1 升汽油。
23+
// - 代表 2 和代表 3 一起到达首都,消耗 1 升汽油。
24+
// - 代表 1 直接到达首都,消耗 1 升汽油。
25+
// - 代表 5 直接到达首都,消耗 1 升汽油。
26+
// - 代表 6 到达城市 4 ,消耗 1 升汽油。
27+
// - 代表 4 和代表 6 一起到达首都,消耗 1 升汽油。
28+
// 最少消耗 7 升汽油。
29+
vector<vector<int>> roads2 = {{3, 1}, {3, 2}, {1, 0}, {0, 4}, {0, 5}, {4, 6}};
30+
int seats2 = 2;
31+
EXPECT_EQ(solution.minimumFuelCost(roads2, seats2), 7);
32+
33+
// 示例 3:
34+
// 输入:roads = [], seats = 1
35+
// 输出:0
36+
// 解释:没有代表需要从别的城市到达首都。
37+
vector<vector<int>> roads3 = {};
38+
int seats3 = 1;
39+
EXPECT_EQ(solution.minimumFuelCost(roads3, seats3), 0);
40+
}

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-04 10:12:54
1+
// 执行编译时间:2023-12-05 10:36:39
22
#include <gtest/gtest.h>
33
#include <lib.hpp>
44

0 commit comments

Comments
 (0)