Skip to content

Commit

Permalink
add: 到达首都的最少油耗
Browse files Browse the repository at this point in the history
  • Loading branch information
yi-ge committed Dec 5, 2023
1 parent 2e0936e commit 45df77f
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 1 deletion.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,10 @@ C++标准库提供的数据结构实在是太多了,参考[C++标准库头文

###

- [到达首都的最少油耗](src/graphs/minimum_fuel_cost_to_report_to_the_capital.cpp) [树, 深度优先搜索, 广度优先搜索, 图]

- LeetCode 2477. 到达首都的最少油耗 <https://leetcode.cn/problems/minimum-fuel-cost-to-report-to-the-capital>

- [T 秒后青蛙的位置](src/graphs/frog_position_after_t_seconds.cpp) [树, 深度优先搜索, 广度优先搜索, 图]

- LeetCode 1377. T 秒后青蛙的位置 <https://leetcode.cn/problems/frog-position-after-t-seconds/>
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
39 changes: 39 additions & 0 deletions src/graphs/minimum_fuel_cost_to_report_to_the_capital.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// 到达首都的最少油耗
// https://leetcode.cn/problems/minimum-fuel-cost-to-report-to-the-capital
// INLINE ../../images/graphs/minimum_fuel_cost_to_report_to_the_capital.jpeg

#include <headers.hpp>

class Solution {
public:
long long int minimumFuelCost(vector<vector<int>> &roads, int seats) {
int n = roads.size() + 1;
vector<vector<int>> graph(n);
for (auto &road : roads) {
int src = road[0], dest = road[1];
graph[src].push_back(dest);
graph[dest].push_back(src);
}

long long int totalCost = 0;
traverse(0, -1, graph, totalCost, seats);
return totalCost;
}

private:
int traverse(int node, int parent, vector<vector<int>> &graph,
long long int &cost, int seats) {
int total = 1;
for (int neighbour : graph[node]) {
if (neighbour != parent) {
int sub_total = traverse(neighbour, node, graph, cost, seats);
cost += (long long int)ceil(
(double)sub_total /
seats); // casting to double for correct division and then
// converting back to long long int
total += sub_total;
}
}
return total;
}
};
40 changes: 40 additions & 0 deletions test/graphs/minimum_fuel_cost_to_report_to_the_capital_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include <graphs/minimum_fuel_cost_to_report_to_the_capital.cpp>

TEST(到达首都的最少油耗, minimumFuelCost) {
Solution solution;
// 示例 1:
// 输入:roads = [[0,1],[0,2],[0,3]], seats = 5
// 输出:3
// 解释:
// - 代表 1 直接到达首都,消耗 1 升汽油。
// - 代表 2 直接到达首都,消耗 1 升汽油。
// - 代表 3 直接到达首都,消耗 1 升汽油。
// 最少消耗 3 升汽油。
vector<vector<int>> roads1 = {{0, 1}, {0, 2}, {0, 3}};
int seats1 = 5;
EXPECT_EQ(solution.minimumFuelCost(roads1, seats1), 3);

// 示例 2:
// 输入:roads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2
// 输出:7
// 解释:
// - 代表 2 到达城市 3 ,消耗 1 升汽油。
// - 代表 2 和代表 3 一起到达城市 1 ,消耗 1 升汽油。
// - 代表 2 和代表 3 一起到达首都,消耗 1 升汽油。
// - 代表 1 直接到达首都,消耗 1 升汽油。
// - 代表 5 直接到达首都,消耗 1 升汽油。
// - 代表 6 到达城市 4 ,消耗 1 升汽油。
// - 代表 4 和代表 6 一起到达首都,消耗 1 升汽油。
// 最少消耗 7 升汽油。
vector<vector<int>> roads2 = {{3, 1}, {3, 2}, {1, 0}, {0, 4}, {0, 5}, {4, 6}};
int seats2 = 2;
EXPECT_EQ(solution.minimumFuelCost(roads2, seats2), 7);

// 示例 3:
// 输入:roads = [], seats = 1
// 输出:0
// 解释:没有代表需要从别的城市到达首都。
vector<vector<int>> roads3 = {};
int seats3 = 1;
EXPECT_EQ(solution.minimumFuelCost(roads3, seats3), 0);
}
2 changes: 1 addition & 1 deletion test/lib/lib_test.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// 执行编译时间:2023-12-04 10:12:54
// 执行编译时间:2023-12-05 10:36:39
#include <gtest/gtest.h>
#include <lib.hpp>

Expand Down

0 comments on commit 45df77f

Please sign in to comment.