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:
- Start with the first city with a cost of C[1].
- 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.
- Calculate cost as abs(j – i) * C[i] and add it to the total cost so far.
- Repeat the previous steps until all the cities have been traversed.
Below is the implementation of the above approach:
C++
// 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; } |
Java
// 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 |
Python3
# 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 |
C#
// 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 |
18
Recommended Posts:
- Minimum cost to connect all cities
- Minimum Cost Graph
- Connect n ropes with minimum cost
- Minimum Cost to cut a board into squares
- Minimum odd cost path in a matrix
- Find the minimum cost to cross the River
- Minimum cost to traverse from one index to another in the String
- Minimum cost to reach the top of the floor by climbing stairs
- Minimum cost to reach a point N from 0 with two different operations allowed
- Choose atleast two elements from array such that their GCD is 1 and cost is minimum
- Minimum cost to process m tasks where switching costs
- Minimum cost to convert str1 to str2 with the given operations
- Minimum cost to reverse edges such that there is path between every pair of nodes
- Minimum cost to make an Array a permutation of first N natural numbers
- Minimum Cost Path with Left, Right, Bottom and Up moves allowed
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.

