📜  查找使用火车到达目的地的最低成本

📅  最后修改于: 2021-09-22 10:05:52             🧑  作者: Mango

一列火车的路线上有N个车站。火车从车站 0 开往 N-1。当 j 大于 i 时,给出了所有车站对 (i, j) 的票价。找出到达目的地的最低成本。
考虑以下示例:

Input: 
cost[N][N] = { {0, 15, 80, 90},
              {INF, 0, 40, 50},
              {INF, INF, 0, 70},
              {INF, INF, INF, 0}
             };
There are 4 stations and cost[i][j] indicates cost to reach j 
from i. The entries where j < i are meaningless.

Output:
The minimum cost is 65
The minimum cost can be obtained by first going to station 1 
from 0. Then from station 1 to station 3.

从 0 到 N-1 的最小成本可以递归地写成如下:

minCost(0, N-1) = MIN { cost[0][n-1],  
                        cost[0][1] + minCost(1, N-1),  
                        minCost(0, 2) + minCost(2, N-1), 
                        ........, 
                        minCost(0, N-2) + cost[N-2][n-1] } 

下面是上述递归公式的实现。

C++
// A naive recursive solution to find min cost path from station 0
// to station N-1
#include
#include
using namespace std;
 
// infinite value
#define INF INT_MAX
 
// Number of stations
#define N 4
 
// A C++ recursive function to find the shortest path from
// source 's' to destination 'd'.
int minCostRec(int cost[][N], int s, int d)
{
    // If source is same as destination
    // or destination is next to source
    if (s == d || s+1 == d)
      return cost[s][d];
 
    // Initialize min cost as direct ticket from
    // source 's' to destination 'd'.
    int min = cost[s][d];
 
    // Try every intermediate vertex to find minimum
    for (int i = s+1; i


Java
// A Java naive recursive solution to find min cost path from station 0
// to station N-1
class shortest_path
{
 
    static int INF = Integer.MAX_VALUE,N = 4;
    // A recursive function to find the shortest path from
    // source 's' to destination 'd'.
    static int minCostRec(int cost[][], int s, int d)
    {
        // If source is same as destination
        // or destination is next to source
        if (s == d || s+1 == d)
          return cost[s][d];
      
        // Initialize min cost as direct ticket from
        // source 's' to destination 'd'.
        int min = cost[s][d];
      
        // Try every intermediate vertex to find minimum
        for (int i = s+1; i


Python
# Python program to find min cost path
# from station 0 to station N-1
 
global N
N = 4
def minCostRec(cost, s, d):
 
    if s == d or s+1 == d:
        return cost[s][d]
 
    min = cost[s][d]
 
    for i in range(s+1, d):
        c = minCostRec(cost,s, i) + minCostRec(cost, i, d)
        if c < min:
            min = c
    return min
 
def minCost(cost):
    return minCostRec(cost, 0, N-1)
cost = [ [0, 15, 80, 90],
         [float("inf"), 0, 40, 50],
         [float("inf"), float("inf"), 0, 70],
         [float("inf"), float("inf"), float("inf"), 0]
        ]
print "The Minimum cost to reach station %d is %d" % \
                                    (N, minCost(cost))
                                     
# This code is contributed by Divyanshu Mehta


C#
// A C# naive recursive solution to find min
// cost path from station 0 to station N-1
using System;
 
class GFG {
     
    static int INF = int.MaxValue, N = 4;
     
    // A recursive function to find the
    // shortest path from source 's' to
    // destination 'd'.
    static int minCostRec(int [,]cost, int s, int d)
    {
         
        // If source is same as destination
        // or destination is next to source
        if (s == d || s + 1 == d)
        return cost[s,d];
     
        // Initialize min cost as direct
        // ticket from source 's' to
        // destination 'd'.
        int min = cost[s,d];
     
        // Try every intermediate vertex to
        // find minimum
        for (int i = s + 1; i < d; i++)
        {
            int c = minCostRec(cost, s, i) +
                       minCostRec(cost, i, d);
                        
            if (c < min)
            min = c;
        }
         
        return min;
    }
     
    // This function returns the smallest
    // possible cost to reach station N-1
    // from station 0. This function mainly
    // uses minCostRec().
    static int minCost(int [,]cost)
    {
        return minCostRec(cost, 0, N-1);
    }
 
    // Driver code
    public static void Main()
    {
        int [,]cost = { {0, 15, 80, 90},
                        {INF, 0, 40, 50},
                        {INF, INF, 0, 70},
                        {INF, INF, INF, 0} };
        Console.WriteLine("The Minimum cost to"
                         + " reach station "+ N
                        + " is "+minCost(cost));
    }
}
 
// This code is contributed by Sam007.


PHP


Javascript


C++
// A Dynamic Programming based solution to find min cost
// to reach station N-1 from station 0.
#include
#include
using namespace std;
 
#define INF INT_MAX
#define N 4
 
// This function returns the smallest possible cost to
// reach station N-1 from station 0.
int minCost(int cost[][N])
{
    // dist[i] stores minimum cost to reach station i
    // from station 0.
    int dist[N];
    for (int i=0; i dist[i] + cost[i][j])
             dist[j] = dist[i] + cost[i][j];
 
    return dist[N-1];
}
 
// Driver program to test above function
int main()
{
    int cost[N][N] = { {0, 15, 80, 90},
                      {INF, 0, 40, 50},
                      {INF, INF, 0, 70},
                      {INF, INF, INF, 0}
                    };
    cout << "The Minimum cost to reach station "
          << N << " is " << minCost(cost);
    return 0;
}


Java
// A Dynamic Programming based solution to find min cost
// to reach station N-1 from station 0.
class shortest_path
{
 
    static int INF = Integer.MAX_VALUE,N = 4;
    // A recursive function to find the shortest path from
    // source 's' to destination 'd'.
     
    // This function returns the smallest possible cost to
    // reach station N-1 from station 0.
    static int minCost(int cost[][])
    {
        // dist[i] stores minimum cost to reach station i
        // from station 0.
        int dist[] = new int[N];
        for (int i=0; i dist[i] + cost[i][j])
                 dist[j] = dist[i] + cost[i][j];
      
        return dist[N-1];
    }
      
 
    public static void main(String args[])
    {
        int cost[][] = { {0, 15, 80, 90},
                      {INF, 0, 40, 50},
                      {INF, INF, 0, 70},
                      {INF, INF, INF, 0}
                    };
        System.out.println("The Minimum cost to reach station "+ N+
                                              " is "+minCost(cost));
    }
 
}/* This code is contributed by Rajat Mishra */


Python3
# A Dynamic Programming based
# solution to find min cost
# to reach station N-1
# from station 0.
 
INF = 2147483647
N = 4
  
# This function returns the
# smallest possible cost to
# reach station N-1 from station 0.
def minCost(cost):
 
    # dist[i] stores minimum
    # cost to reach station i
    # from station 0.
    dist=[0 for i in range(N)]
    for i in range(N):
        dist[i] = INF
    dist[0] = 0
  
    # Go through every station
    # and check if using it
    # as an intermediate station
    # gives better path
    for i in range(N):
        for j in range(i+1,N):
            if (dist[j] > dist[i] + cost[i][j]):
                dist[j] = dist[i] + cost[i][j]
  
    return dist[N-1]
 
  
# Driver program to
# test above function
 
cost= [ [0, 15, 80, 90],
            [INF, 0, 40, 50],
            [INF, INF, 0, 70],
            [INF, INF, INF, 0]]
             
print("The Minimum cost to reach station ",
           N," is ",minCost(cost))
 
# This code is contributed
# by Anant Agarwal.


C#
// A Dynamic Programming based solution
// to find min cost to reach station N-1
// from station 0.
using System;
 
class GFG {
     
    static int INF = int.MaxValue, N = 4;
    // A recursive function to find the
    // shortest path from source 's' to
    // destination 'd'.
     
    // This function returns the smallest
    // possible cost to reach station N-1
    // from station 0.
    static int minCost(int [,]cost)
    {
         
        // dist[i] stores minimum cost
        // to reach station i from
        // station 0.
        int []dist = new int[N];
         
        for (int i = 0; i < N; i++)
            dist[i] = INF;
             
        dist[0] = 0;
     
        // Go through every station and check
        // if using it as an intermediate
        // station gives better path
        for (int i = 0; i < N; i++)
            for (int j = i + 1; j < N; j++)
                if (dist[j] > dist[i] + cost[i,j])
                    dist[j] = dist[i] + cost[i,j];
     
        return dist[N-1];
    }
     
 
    public static void Main()
    {
        int [,]cost = { {0, 15, 80, 90},
                        {INF, 0, 40, 50},
                        {INF, INF, 0, 70},
                        {INF, INF, INF, 0} };
        Console.WriteLine("The Minimum cost to"
                        + " reach station "+ N
                       + " is "+minCost(cost));
    }
}
 
// This code is contributed by Sam007.


PHP
 $dist[$i] + $cost[$i][$j])
            $dist[$j] = $dist[$i] + $cost[$i][$j];
 
    return $dist[$N-1];
}
 
// Driver program to test above function
 
    $cost =array(array(0, 15, 80, 90),
            array(INF, 0, 40, 50),
            array(INF, INF, 0, 70),
            array(INF, INF, INF, 0));
    echo  "The Minimum cost to reach station ",
        $N , " is ",minCost($cost);
     
 
?>


Javascript


输出:

The Minimum cost to reach station 4 is 65

上述实现的时间复杂度是指数级的,因为它尝试了从 0 到 N-1 的所有可能路径。上述解决方案多次解决相同的子问题(通过为 minCostPathRec(0, 5) 绘制递归树可以看出。
由于这个问题具有动态规划问题的两个性质((见this和this)。与其他典型的动态规划(DP)问题一样,通过存储子问题的解并以自下而上的方式解决问题,可以避免相同子问题的重新计算.
一种动态规划解决方案是创建一个二维表并使用上面给出的递归公式填充该表。此解决方案所需的额外空间为 O(N 2 ),时间复杂度为 O(N 3 )
我们可以使用 O(N) 额外空间和 O(N 2 ) 时间来解决这个问题。该想法基于给定输入矩阵是有向无环图 (DAG) 的事实。 DAG 中的最短路径可以使用下面文章中讨论的方法计算。
有向无环图中的最短路径
与上面提到的帖子相比,我们需要在这里做更少的工作,因为我们知道图的拓扑排序。这里顶点的拓扑排序是0, 1, …, N-1。以下是已知拓扑排序后的想法。
下面代码的想法是首先计算站点 1 的最小成本,然后是站点 2,依此类推。这些成本存储在数组 dist[0…N-1] 中。
1) 站点 0 的最小成本为 0,即 dist[0] = 0
2) 站点 1 的最小成本是 cost[0][1],即 dist[1] = cost[0][1]
3) 站点 2 的最小成本是以下两个中的最小值。
a) dist[0] + cost[0][2]
b) 分布[1] + 成本[1][2]
3) 站点 3 的最小成本是以下三个中的最小值。
a) dist[0] + cost[0][3]
b) 分布[1] + 成本[1][3]
c) 分布[2] + 成本[2][3]
类似地,计算dist[4]、dist[5]、……dist[N-1]。
下面是上述想法的实现。

C++

// A Dynamic Programming based solution to find min cost
// to reach station N-1 from station 0.
#include
#include
using namespace std;
 
#define INF INT_MAX
#define N 4
 
// This function returns the smallest possible cost to
// reach station N-1 from station 0.
int minCost(int cost[][N])
{
    // dist[i] stores minimum cost to reach station i
    // from station 0.
    int dist[N];
    for (int i=0; i dist[i] + cost[i][j])
             dist[j] = dist[i] + cost[i][j];
 
    return dist[N-1];
}
 
// Driver program to test above function
int main()
{
    int cost[N][N] = { {0, 15, 80, 90},
                      {INF, 0, 40, 50},
                      {INF, INF, 0, 70},
                      {INF, INF, INF, 0}
                    };
    cout << "The Minimum cost to reach station "
          << N << " is " << minCost(cost);
    return 0;
}

Java

// A Dynamic Programming based solution to find min cost
// to reach station N-1 from station 0.
class shortest_path
{
 
    static int INF = Integer.MAX_VALUE,N = 4;
    // A recursive function to find the shortest path from
    // source 's' to destination 'd'.
     
    // This function returns the smallest possible cost to
    // reach station N-1 from station 0.
    static int minCost(int cost[][])
    {
        // dist[i] stores minimum cost to reach station i
        // from station 0.
        int dist[] = new int[N];
        for (int i=0; i dist[i] + cost[i][j])
                 dist[j] = dist[i] + cost[i][j];
      
        return dist[N-1];
    }
      
 
    public static void main(String args[])
    {
        int cost[][] = { {0, 15, 80, 90},
                      {INF, 0, 40, 50},
                      {INF, INF, 0, 70},
                      {INF, INF, INF, 0}
                    };
        System.out.println("The Minimum cost to reach station "+ N+
                                              " is "+minCost(cost));
    }
 
}/* This code is contributed by Rajat Mishra */

蟒蛇3

# A Dynamic Programming based
# solution to find min cost
# to reach station N-1
# from station 0.
 
INF = 2147483647
N = 4
  
# This function returns the
# smallest possible cost to
# reach station N-1 from station 0.
def minCost(cost):
 
    # dist[i] stores minimum
    # cost to reach station i
    # from station 0.
    dist=[0 for i in range(N)]
    for i in range(N):
        dist[i] = INF
    dist[0] = 0
  
    # Go through every station
    # and check if using it
    # as an intermediate station
    # gives better path
    for i in range(N):
        for j in range(i+1,N):
            if (dist[j] > dist[i] + cost[i][j]):
                dist[j] = dist[i] + cost[i][j]
  
    return dist[N-1]
 
  
# Driver program to
# test above function
 
cost= [ [0, 15, 80, 90],
            [INF, 0, 40, 50],
            [INF, INF, 0, 70],
            [INF, INF, INF, 0]]
             
print("The Minimum cost to reach station ",
           N," is ",minCost(cost))
 
# This code is contributed
# by Anant Agarwal.

C#

// A Dynamic Programming based solution
// to find min cost to reach station N-1
// from station 0.
using System;
 
class GFG {
     
    static int INF = int.MaxValue, N = 4;
    // A recursive function to find the
    // shortest path from source 's' to
    // destination 'd'.
     
    // This function returns the smallest
    // possible cost to reach station N-1
    // from station 0.
    static int minCost(int [,]cost)
    {
         
        // dist[i] stores minimum cost
        // to reach station i from
        // station 0.
        int []dist = new int[N];
         
        for (int i = 0; i < N; i++)
            dist[i] = INF;
             
        dist[0] = 0;
     
        // Go through every station and check
        // if using it as an intermediate
        // station gives better path
        for (int i = 0; i < N; i++)
            for (int j = i + 1; j < N; j++)
                if (dist[j] > dist[i] + cost[i,j])
                    dist[j] = dist[i] + cost[i,j];
     
        return dist[N-1];
    }
     
 
    public static void Main()
    {
        int [,]cost = { {0, 15, 80, 90},
                        {INF, 0, 40, 50},
                        {INF, INF, 0, 70},
                        {INF, INF, INF, 0} };
        Console.WriteLine("The Minimum cost to"
                        + " reach station "+ N
                       + " is "+minCost(cost));
    }
}
 
// This code is contributed by Sam007.

PHP

 $dist[$i] + $cost[$i][$j])
            $dist[$j] = $dist[$i] + $cost[$i][$j];
 
    return $dist[$N-1];
}
 
// Driver program to test above function
 
    $cost =array(array(0, 15, 80, 90),
            array(INF, 0, 40, 50),
            array(INF, INF, 0, 70),
            array(INF, INF, INF, 0));
    echo  "The Minimum cost to reach station ",
        $N , " is ",minCost($cost);
     
 
?>

Javascript


输出:

The Minimum cost to reach station 4 is 65

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程