📌  相关文章
📜  在不连续两次重复执行相同任务的情况下,将总成本降至最低

📅  最后修改于: 2021-04-30 02:49:19             🧑  作者: Mango

给定大小为MXN的数组arr [] [] ,其中M表示任务数, N表示迭代数。数组arr [i] [j]中的一项表示在第i迭代中执行任务j的成本。假设无法在两个连续的迭代中计算出相同的任务j ,则任务是计算在每次迭代中恰好执行一项任务的最小成本。

例子:

幼稚的方法:针对此问题的幼稚的方法是生成所有可能的任务组合,然后以最小的成本搜索组合。但是,这对于较大尺寸的矩阵将失败,因为此方法的时间复杂度将为O(M N )。

高效的方法:通过使用动态编程的概念可以有效地解决此问题。直觉是形成尺寸为N x M的dp表dp [] [] ,其中dp [i] [j]代表i迭代中j任务的最小成本。但是,由于不应连续两天重复执行同一任务,因此可以通过以下方式填充dp表:

 dp[i][j] = cost[i][j] + \min_{j!=k}(dp[i-1][k])

dp [] []数组的第一行将与cost [] []矩阵的第一行相同。答案是最后一行的最小元素。

下面是上述方法的实现:

CPP
// C++ implementation of the above approach
// Function to return the minimum cost
// for N iterations
#include 
using namespace std;
  
int findCost(vector>cost_mat, int N, int M)
{
    // Construct the dp table
    vector> dp(N,vector(M, 0));
      
    // 1st row of dp table will be equal
    // to the 1st of cost matrix
  
    for(int i = 0; i < M; i++)
        dp[0][i] = cost_mat[0][i];
      
    // Iterate through all the rows
    for (int row = 1; row < N; row++){
          
        // To iterate through the
        // columns of current row
        for (int curr_col = 0; curr_col < M; curr_col++)
        {
  
            // Initialize val as infinity
            int val = 999999999;
  
            // To iterate through the
            // columns of previous row
            for(int prev_col = 0; prev_col < M; prev_col++)
            {
  
                if (curr_col != prev_col)
                    val = min(val, dp[row - 1][prev_col]);
            }
              
            // Fill the dp matrix
            dp[row][curr_col] = val + cost_mat[row][curr_col];
        }
        }
  
    // Returning the minimum value
    int ans = INT_MAX;
    for(int i = 0; i < M; i++)
        ans = min(ans, dp[N-1][i]);
    return ans;
}
  
// Driver code
int main()
{
      
// Number of iterations
int N = 4;
  
// Number of tasks
int M = 4;
  
// Cost matrix
vector> cost_mat;
cost_mat = {{4, 5, 3, 2},
            {6, 2, 8, 1},
            {6, 2, 2, 1},
            {0, 5, 5, 1}};
  
cout << findCost(cost_mat, N, M);
return 0;
}
  
// This code is contributed by mohit kumar 29


Java
// Java implementation of the above approach
// Function to return the minimum cost
// for N iterations
import java.io.*; 
class GFG {
  
    static int findCost(int cost_mat[][], int N, int M)
    {
        // Construct the dp table
        int dp[][] = new int[N][M] ;
         
      
        // 1st row of dp table will be equal
        // to the 1st of cost matrix
  
        for(int i = 0; i < M; i++)
            dp[0][i] = cost_mat[0][i];
      
        // Iterate through all the rows
        for (int row = 1; row < N; row++){
          
            // To iterate through the
            // columns of current row
            for (int curr_col = 0; curr_col < M; curr_col++)
            {
  
                // Initialize val as infinity
                int val = 999999999;
  
                // To iterate through the
                // columns of previous row
                for(int prev_col = 0; prev_col < M; prev_col++)
                {
  
                    if (curr_col != prev_col)
                        val = Math.min(val, dp[row - 1][prev_col]);
                }
              
                // Fill the dp matrix
                dp[row][curr_col] = val + cost_mat[row][curr_col];
            }
            }
  
        // Returning the minimum value
        int ans = Integer.MAX_VALUE;
        for(int i = 0; i < M; i++)
            ans = Math.min(ans, dp[N-1][i]);
        return ans;
    }
  
    // Driver code
    public static void main (String[] args)  
    {
      
    // Number of iterations
    int N = 4;
  
    // Number of tasks
    int M = 4;
  
    // Cost matrix
    int cost_mat[][] = {{4, 5, 3, 2},
                {6, 2, 8, 1},
                {6, 2, 2, 1},
                {0, 5, 5, 1}};
  
    System.out.println(findCost(cost_mat, N, M));
      
    }
  
}
  
  
// This code is contributed by ANKITKUMAR34


Python
# Python implementation of the above approach
  
# Function to return the minimum cost 
# for N iterations
def findCost(cost_mat, N, M):
      
    # Construct the dp table
    dp = [[0]*M for _ in range(M)]
      
    # 1st row of dp table will be equal 
    # to the 1st of cost matrix 
    dp[0] = cost_mat[0]
      
       
    # Iterate through all the rows
    for row in range(1, N):
          
        # To iterate through the 
        # columns of current row
        for curr_col in range(M):
              
            # Initialize val as infinity
            val = 999999999
              
            # To iterate through the 
            # columns of previous row
            for prev_col in range(M):
                  
                if curr_col != prev_col: 
                    val = min(val, dp[row-1][prev_col])
                      
            # Fill the dp matrix
            dp[row][curr_col] = val + cost_mat[row][curr_col]
              
    # Returning the minimum value
    return min(dp[-1])
                  
if __name__ == "__main__":
  
    # Number of iterations
    N = 4
      
    # Number of tasks
    M = 4
  
    # Cost matrix
    cost_mat = [[4, 5, 3, 2],
                [6, 2, 8, 1],
                [6, 2, 2, 1],
                [0, 5, 5, 1]]
      
    print(findCost(cost_mat, N, M))


C#
// C# implementation of the above approach
// Function to return the minimum cost
// for N iterations
using System;
  
class GFG {
  
    static int findCost(int [,]cost_mat, int N, int M)
    {
        // Construct the dp table
        int [,]dp = new int[N, M] ;
         
      
        // 1st row of dp table will be equal
        // to the 1st of cost matrix
  
        for(int i = 0; i < M; i++)
            dp[0, i] = cost_mat[0, i];
      
        // Iterate through all the rows
        for (int row = 1; row < N; row++){
          
            // To iterate through the
            // columns of current row
            for (int curr_col = 0; curr_col < M; curr_col++)
            {
  
                // Initialize val as infinity
                int val = 999999999;
  
                // To iterate through the
                // columns of previous row
                for(int prev_col = 0; prev_col < M; prev_col++)
                {
  
                    if (curr_col != prev_col)
                        val = Math.Min(val, dp[row - 1, prev_col]);
                }
              
                // Fill the dp matrix
                dp[row, curr_col] = val + cost_mat[row, curr_col];
            }
            }
  
        // Returning the minimum value
        int ans = int.MaxValue;
          
        for(int i = 0; i < M; i++)
            ans = Math.Min(ans, dp[N - 1, i]);
              
        return ans;
    }
  
    // Driver code
    public static void Main (string[] args) 
    {
      
        // Number of iterations
        int N = 4;
      
        // Number of tasks
        int M = 4;
      
        // Cost matrix
        int [,]cost_mat = {{4, 5, 3, 2},
                    {6, 2, 8, 1},
                    {6, 2, 2, 1},
                    {0, 5, 5, 1}};
      
        Console.WriteLine(findCost(cost_mat, N, M));
      
    }
  
}
  
// This code is contributed by Yash_R


输出:
5