📜  在N个城市之间的最低可能旅行费用

📅  最后修改于: 2021-05-04 14:57:15             🧑  作者: Mango

直路上有N个城市,每个城市之间的距离为1个单位。您必须搭乘公交车才能到达第(N + 1)城市。第i城市每行驶1单位距离将花费C [i]美元。换句话说,从第i城市到第j城市的旅行成本为abs(i – j)* C [i]美元。任务是找到从城市1到城市(N +1),即超过最后一个城市的最低旅行成本。
例子:

方法:方法非常简单,只需乘坐迄今为止成本最低的公交车即可。只要找到成本更低的公共汽车,就从该城市换公共汽车。以下是要解决的步骤:

  1. 从第一个城市开始,其成本为C [1]
  2. 旅行到下一个城市,直到具有城市Ĵ成本小于前市(由我们旅行的时候,我们说城市i)发觉。
  3. 将成本计算为abs(j – i)* C [i] ,并将其加到目前的总成本中。
  4. 重复前面的步骤,直到遍历所有城市。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function to return the minimum cost to
// travel from the first city to the last
int minCost(vector& 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 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


Javascript


输出:
18