Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
1.1 kB
0
Indexable
Never
def get_minimum_cost(machine_count, final_machine_count, shifting_cost):
    
    n = len(machine_count)
    dp = [[-1 for _ in range(1001)] for _ in range(1001)]

    def solve(i, target):
       
        if dp[i][sum(target)] != -1:
            return dp[i][sum(target)]

        if all(t == 0 for t in target):
            return 0

        min_cost = float('inf')
        
        for j in range(n):
            if i == j: 
                continue

            move_cost = (machine_count[i] - target[i]) * shifting_cost
            remaining_target = target.copy()
            remaining_target[j] += machine_count[i] - target[i]

            recursive_cost = solve(i + 1, remaining_target)
            min_cost = min(min_cost, move_cost + recursive_cost)

        dp[i][sum(target)] = min_cost

        return min_cost

    return solve(0, final_machine_count.copy())

machine_count = [2, 3, 5, 7]
final_machine_count = [5, 10, 5]
shifting_cost = 2

minimum_cost = get_minimum_cost(machine_count, final_machine_count, shifting_cost)
print(minimum_cost)
Leave a Comment