Skip to content
DeepForgeby svx
All problems
EasyCalculusca-226

Optimal Transport Cost

Problem

The cost of a transport plan P with cost matrix C is the Frobenius inner product:

cost = sum over i, j of P_ij * C_ij

Given the plan and the cost matrix, return the total transport cost.

Hint

The optimal plan minimizes this inner product subject to the marginal constraints.

Test cases

case 1
in: [[[0.5,0.5],[0,0]],[[1,2],[3,4]]]
expected: 1.5
case 2
in: [[[0,1],[1,0]],[[2,5],[7,3]]]
expected: 12
case 3
in: [[[1]],[[4]]]
expected: 4
case 4
in: [[[0.25,0.25],[0.25,0.25]],[[1,2],[3,4]]]
expected: 2.5
Starter codePython
def optimal_transport_cost(plan, cost):
    # Your code here
    pass

4 test cases · run your code against them in the editor