-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
84 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
40
test/graphs/minimum_fuel_cost_to_report_to_the_capital_test.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
|
||
|