Minimum possible travel cost among N cities

There are N cities situated on a straight road and each is separated by a distance of 1 unit. You have to reach the (N + 1)th city by boarding a bus. The ith city would cost of C[i] dollars to travel 1 unit of distance. In other words, cost to travel from the ith city to the jth city is abs(i – j ) * C[i] dollars. The task is to find the minimum cost to travel from city 1 to city (N + 1) i.e. beyond the last city.

Examples:

Input: C[] = {3, 5, 4}
Output: 9
The bus boarded from the first city has the minimum
cost of all so it will be used to travel (N + 1) unit.



Input: C[] = {4, 7, 8, 3, 4}
Output: 18
Board the bus at the first city then change
the bus at the fourth city.
(3 * 4) + (2 * 3) = 12 + 6 = 18

Approach: The approach is very simple, just travel by the bus which has the lowest cost so far. Whenever a bus with an even lower cost is found, change the bus from that city. Following are the steps to solve:

  1. Start with the first city with a cost of C[1].
  2. Travel to the next city until a city j having cost less than the previous city (by which we are travelling, let’s say city i) is found.
  3. Calculate cost as abs(j – i) * C[i] and add it to the total cost so far.
  4. Repeat the previous steps until all the cities have been traversed.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to return the minimum cost to
// travel from the first city to the last
int minCost(vector<int>& cost, int n)
{
  
    // To store the total cost
    int totalCost = 0;
  
    // Start from the first city
    int boardingBus = 0;
  
    for (int i = 1; i < n; i++) {
  
        // If found any city with cost less than
        // that of the previous boarded
        // bus then change the bus
        if (cost[boardingBus] > cost[i]) {
  
            // Calculate the cost to travel from
            // the currently boarded bus
            // till the current city
            totalCost += ((i - boardingBus) * cost[boardingBus]);
  
            // Update the currently boarded bus
            boardingBus = i;
        }
    }
  
    // Finally calculate the cost for the
    // last boarding bus till the (N + 1)th city
    totalCost += ((n - boardingBus) * cost[boardingBus]);
    return totalCost;
}
  
// Driver code
int main()
{
    vector<int> cost{ 4, 7, 8, 3, 4 };
    int n = cost.size();
  
    cout << minCost(cost, n);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach
class GFG
{
  
// Function to return the minimum cost to
// travel from the first city to the last
static int minCost(int []cost, int n)
{
  
    // To store the total cost
    int totalCost = 0;
  
    // Start from the first city
    int boardingBus = 0;
  
    for (int i = 1; i < n; i++)
    {
  
        // If found any city with cost less than
        // that of the previous boarded
        // bus then change the bus
        if (cost[boardingBus] > cost[i]) 
        {
  
            // Calculate the cost to travel from
            // the currently boarded bus
            // till the current city
            totalCost += ((i - boardingBus) * cost[boardingBus]);
  
            // Update the currently boarded bus
            boardingBus = i;
        }
    }
  
    // Finally calculate the cost for the
    // last boarding bus till the (N + 1)th city
    totalCost += ((n - boardingBus) * cost[boardingBus]);
    return totalCost;
}
  
// Driver code
public static void main(String[] args)
{
    int []cost = { 4, 7, 8, 3, 4 };
    int n = cost.length;
  
    System.out.print(minCost(cost, n));
}
}
  
// This code is contributed by PrinciRaj1992

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of the approach
  
# Function to return the minimum cost to
# travel from the first city to the last
def minCost(cost, n):
  
    # To store the total cost
    totalCost = 0
  
    # Start from the first city
    boardingBus = 0
  
    for i in range(1, n):
  
        # If found any city with cost less than
        # that of the previous boarded
        # bus then change the bus
        if (cost[boardingBus] > cost[i]):
  
            # Calculate the cost to travel from
            # the currently boarded bus
            # till the current city
            totalCost += ((i - boardingBus) * 
                          cost[boardingBus])
  
            # Update the currently boarded bus
            boardingBus = i
  
    # Finally calculate the cost for the
    # last boarding bus till the (N + 1)th city
    totalCost += ((n - boardingBus) * 
                  cost[boardingBus])
    return totalCost
  
# Driver code
cost = [ 4, 7, 8, 3, 4]
n = len(cost)
  
print(minCost(cost, n))
  
# This code is contributed by Mohit Kumar 

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the approach
using System;
  
class GFG
{
  
// Function to return the minimum cost to
// travel from the first city to the last
static int minCost(int []cost, int n)
{
  
    // To store the total cost
    int totalCost = 0;
  
    // Start from the first city
    int boardingBus = 0;
  
    for (int i = 1; i < n; i++)
    {
  
        // If found any city with cost less than
        // that of the previous boarded
        // bus then change the bus
        if (cost[boardingBus] > cost[i]) 
        {
  
            // Calculate the cost to travel from
            // the currently boarded bus
            // till the current city
            totalCost += ((i - boardingBus) * 
                          cost[boardingBus]);
  
            // Update the currently boarded bus
            boardingBus = i;
        }
    }
  
    // Finally calculate the cost for the
    // last boarding bus till the (N + 1)th city
    totalCost += ((n - boardingBus) * 
                  cost[boardingBus]);
    return totalCost;
}
  
// Driver code
public static void Main(String[] args)
{
    int []cost = { 4, 7, 8, 3, 4 };
    int n = cost.Length;
  
    Console.Write(minCost(cost, n));
}
}
  
// This code is contributed by 29AjayKumar

chevron_right


Output:

18

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.