Similar Problems

Similar Problems not available

Minimum Fuel Cost To Report To The Capital - Leetcode Solution

Companies:

LeetCode:  Minimum Fuel Cost To Report To The Capital Leetcode Solution

Difficulty: Medium

Topics: breadth-first-search tree depth-first-search graph  

Problem Statement:

Given a map of cities connected by roads and the cost of fuel to travel from one city to another, the minimum fuel cost to report to the capital is to be computed. The fuel cost to report to a city is the cost of fuel multiplied by the distance from that city to the capital. At any city, you can refuel the car with an unlimited amount of fuel, but the car has limited tank capacity.

Solution:

We can solve this problem using Dijkstra’s Algorithm.

In Dijkstra’s algorithm, we maintain a priority queue to keep track of the next shortest distance node from the source node. Initially, the distance of the source node is set to 0 and all other nodes are set to infinity. We extract the node with the shortest distance from the priority queue, and then update the distance of its neighbors. We repeat this process until all nodes are visited.

In this problem, we can modify Dijkstra’s Algorithm to keep track of the minimum fuel cost to report to the capital. We can maintain a priority queue to keep track of the next city with the minimum fuel cost to report to the capital. Initially, we set the fuel cost of the capital to 0, and fuel costs of all other cities to infinity. We extract the city with the minimum fuel cost from the priority queue, and then update the fuel costs of its neighbors based on the fuel cost of the current city, cost of fuel required to travel from the current city to its neighbors, and distance of the neighbors from the capital.

Algorithm:

  1. Initialize the minimum fuel cost table with 0 for the capital and infinity for all other cities.
  2. Add the capital to the priority queue with its fuel cost as 0.
  3. While the priority queue is not empty, extract the city with the minimum fuel cost.
  4. For each neighbor of the current city, calculate the fuel cost of traveling to the neighbor based on the current city’s fuel cost, the fuel cost required to travel from the current city to the neighbor, and the distance of the neighbor from the capital.
  5. If the calculated fuel cost is less than the existing fuel cost of the neighbor, update the fuel cost of the neighbor with the calculated fuel cost and add the neighbor to the priority queue.

Complexity Analysis:

The time complexity of our algorithm can be calculated as O(ElogV), where E is the number of edges and V is the number of vertices. The logarithmic term arises from the priority queue operations.

The space complexity of our algorithm is O(V), where V is the number of vertices. We need to store the fuel cost table and priority queue.

Code:

The following is the Python code for our algorithm:

import heapq from typing import List

def minFuelCost(n: int, roads: List[List[int]], fuelPrices: List[int]) -> int: # Create adjacency list and fuel cost table adj = [[] for _ in range(n)] fuel_cost = [float('inf') for _ in range(n)] for u, v, d in roads: adj[u-1].append((v-1, d)) adj[v-1].append((u-1, d))

# Initialize the fuel cost table and priority queue
fuel_cost[0] = 0
pq = [(0, 0)]

# Dijkstra's algorithm with fuel cost modification
while pq:
    cost, u = heapq.heappop(pq)
    if fuel_cost[u] < cost:
        continue
    for v, d in adj[u]:
        fuel_needed = d * fuelPrices[u]
        fc = cost + fuel_needed * (v != 0)
        if fc < fuel_cost[v]:
            fuel_cost[v] = fc
            heapq.heappush(pq, (fc, v))

# Return the fuel cost to reach the capital
return fuel_cost[-1]

Minimum Fuel Cost To Report To The Capital Solution Code

1