## C# || How To Find The Minimum Fuel Cost To Report To The Capital Using C#

The following is a module with functions which demonstrates how to find the minimum fuel cost to report to the capital using C#.

1. Minimum Fuel Cost – Problem Statement

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] = [a _{i}, b_{i}]** denotes that there exists a

**bidirectional road**connecting cities

**a**and

_{i}**b**.

_{i}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:

- Representative_{1}goes directly to the capital with 1 liter of fuel.

- Representative_{2}goes directly to the capital with 1 liter of fuel.

- Representative_{3}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:

- Representative_{2}goes directly to city 3 with 1 liter of fuel.

- Representative_{2}and representative_{3}go together to city 1 with 1 liter of fuel.

- Representative_{2}and representative_{3}go together to the capital with 1 liter of fuel.

- Representative_{1}goes directly to the capital with 1 liter of fuel.

- Representative_{5}goes directly to the capital with 1 liter of fuel.

- Representative_{6}goes directly to city 4 with 1 liter of fuel.

- Representative_{4}and representative_{6}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.

2. Minimum Fuel Cost – Solution

The following is a solution which demonstrates how to find the minimum fuel cost to report to the capital.

```
```
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
// ============================================================================// Author: Kenneth Perkins// Date: May 24, 2023// Taken From: http://programmingnotes.org/// File: Solution.cs// Description: Demonstrates how to find minimum fuel cost to the capital// ============================================================================public class Solution { public long MinimumFuelCost(int[][] roads, int seats) { int n = roads.Length + 1; var adj = new Dictionary<int, List<int>>(); int[] degree = new int[n]; foreach (int[] road in roads) { if (!adj.ContainsKey(road[0])) { adj[road[0]] = new List<int>(); } adj[road[0]].Add(road[1]); if (!adj.ContainsKey(road[1])) { adj[road[1]] = new List<int>(); } adj[road[1]].Add(road[0]); ++degree[road[0]]; ++degree[road[1]]; } return BFS(n, adj, degree, seats); } public long BFS(int n, Dictionary<int, List<int>> adj, int[] degree, int seats) { var q = new Queue<int>(); for (int i = 1; i < n; i++) { if (degree[i] == 1) { q.Enqueue(i); } } int[] representatives = new int[n]; Array.Fill(representatives, 1); long fuel = 0; while (q.Count > 0) { int node = q.Dequeue(); fuel += (long)Math.Ceiling((double) representatives[node] / seats); foreach (int neighbor in adj[node]) { --degree[neighbor]; representatives[neighbor] += representatives[node]; if (degree[neighbor] == 1 && neighbor != 0) { q.Enqueue(neighbor); } } } return fuel; }}// http://programmingnotes.org/

**QUICK NOTES**:

The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

Once compiled, you should get this as your output for the example cases:

3

7

0

## Leave a Reply