LeetCode2477. Minimum Fuel Cost to Report to the Capital

2023-12-06 16:52

本文主要是介绍LeetCode2477. Minimum Fuel Cost to Report to the Capital,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

文章目录

    • 一、题目
    • 二、题解

一、题目

There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of n cities numbered from 0 to n - 1 and exactly n - 1 roads. The capital city is city 0. You are given a 2D integer array roads where roads[i] = [ai, bi] denotes that there exists a bidirectional road connecting cities ai and bi.

There is a meeting for the representatives of each city. The meeting is in the capital city.

There is a car in each city. You are given an integer seats that indicates the number of seats in each car.

A representative can use the car in their city to travel or change the car and ride with another representative. The cost of traveling between two cities is one liter of fuel.

Return the minimum number of liters of fuel to reach the capital city.

Example 1:

Input: roads = [[0,1],[0,2],[0,3]], seats = 5
Output: 3
Explanation:

  • Representative1 goes directly to the capital with 1 liter of fuel.
  • Representative2 goes directly to the capital with 1 liter of fuel.
  • Representative3 goes directly to the capital with 1 liter of fuel.
    It costs 3 liters of fuel at minimum.
    It can be proven that 3 is the minimum number of liters of fuel needed.
    Example 2:

Input: roads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2
Output: 7
Explanation:

  • Representative2 goes directly to city 3 with 1 liter of fuel.
  • Representative2 and representative3 go together to city 1 with 1 liter of fuel.
  • Representative2 and representative3 go together to the capital with 1 liter of fuel.
  • Representative1 goes directly to the capital with 1 liter of fuel.
  • Representative5 goes directly to the capital with 1 liter of fuel.
  • Representative6 goes directly to city 4 with 1 liter of fuel.
  • Representative4 and representative6 go together to the capital with 1 liter of fuel.
    It costs 7 liters of fuel at minimum.
    It can be proven that 7 is the minimum number of liters of fuel needed.
    Example 3:

Input: roads = [], seats = 1
Output: 0
Explanation: No representatives need to travel to the capital city.

Constraints:

1 <= n <= 105
roads.length == n - 1
roads[i].length == 2
0 <= ai, bi < n
ai != bi
roads represents a valid tree.
1 <= seats <= 105

二、题解

class Solution {
public:long long res = 0;int dfs(int x,int father,vector<vector<int>>& g,int seats){int size = 1;for(int i = 0;i < g[x].size();i++){if(g[x][i] != father){size += dfs(g[x][i],x,g,seats);}}if(x){res += (size - 1) / seats + 1;}return size;}long long minimumFuelCost(vector<vector<int>>& roads, int seats) {vector<vector<int>> g(roads.size()+1);//记录每个节点的邻居for(int i = 0;i < roads.size();i++){int x = roads[i][0];int y = roads[i][1];g[x].push_back(y);g[y].push_back(x);}dfs(0,-1,g,seats);return res;}
};

这篇关于LeetCode2477. Minimum Fuel Cost to Report to the Capital的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/462556

相关文章

【Hdu】Minimum Inversion Number(逆序,线段树)

利用线段树在nlogn的时间复杂度内求一段数的逆序。 由于给的序列是由0 ~ n -1组成的,求出初始的逆序之后可以递推出移动之后的逆序数。 #include<cstdio>#include<iostream>#include<cstring>#include<algorithm>using namespace std;typedef long long LL;const in

11GR2下基于CBO全表扫描cost计算

########################################################## ##11gr2下基于cbo优化器,在不做系统统计信息收集下全表扫描的成本计算#### ########################################################## CBO的成本计算设计到非工作负载下的系统统计信息 CPUSPEEDNW=>系统

【ArcGIS Pro实操第二期】最小成本路径(Least-cost path)原理及实操案例

ArcGIS Pro实操第一期:最小成本路径原理及实操案例 概述(Creating the least-cost path)1.1 原理介绍1.2 实现步骤1.3 应用案例 2 GIS实操2.1 工具箱简介2.1.1 成本路径(Cost path)2.1.2 成本距离(Cost distance)2.1.2 路径距离(Path Distance) 2.2 案例: 参考 概述(Cre

The Prompt Report 2

The Prompt Report 提示工程调查报告《The Prompt Report: A Systematic Survey of Prompting Techniques》 主要内容 Core Prompting Techniques Text based Techniques:PRISMA流程,58中基于文本的提示技术,提示语术语分类表;MLT:Multilingual T

【ArcGIS Pro实操第一期】最小成本路径(Least-cost path)原理及实操案例

ArcGIS Pro实操第一期:最小成本路径原理及实操案例 概述(Creating the least-cost path)1.1 原理介绍1.2 实现步骤1.3 应用案例 2 GIS实操2.1 工具箱简介2.1.1 成本路径(Cost path)2.1.2 成本距离(Cost distance)2.1.2 路径距离(Path Distance) 2.2 案例: 参考 概述(Cre

[LeetCode] 64. Minimum Path Sum

题:https://leetcode.com/problems/minimum-path-sum/description/ 题目 Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers

Path With Maximum Minimum Value

Given a matrix of integers A with R rows and C columns, find the maximum score of a path starting at [0,0] and ending at [R-1,C-1]. The score of a path is the minimum value in that path.  For example

路径优化 minimum-snap(对A*的全局路径进行优化)

实现效果: 介绍: 使用Astar进行路径规划,使用minimum-snap进行路径优化处理,建议参考文章: 【附源码和详细的公式推导】Minimum Snap轨迹生成,闭式求解Minimum Snap问题,机器人轨迹优化,多项式轨迹路径生成与优化-CSDN博客 参考代码: AllocateTime:zm0612 Minimum-Snap https://github.com/zm0

leetcode209. Minimum Size Subarray Sum

题目 Given an array of positive integers nums and a positive integer target, return the minimal length of a subarray whose sum is greater than or equal to target. If there is no such subarray, return 0

Leetcode 3273. Minimum Amount of Damage Dealt to Bob

Leetcode 3273. Minimum Amount of Damage Dealt to Bob 1. 解题思路2. 代码实现 题目链接:3273. Minimum Amount of Damage Dealt to Bob 1. 解题思路 这一题代码并不复杂,关键就是想明白为啥。 我直接的一个思路就是贪婪算法,考察任意两个怪 i , j i, j i,j之间处理的先后关系,其结果