📌  相关文章
📜  在两次连续迭代中不重复相同任务的情况下最小化总成本

📅  最后修改于: 2021-09-17 07:45:46             🧑  作者: 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


Javascript


输出:
5

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