
Input:
'N' = 3
‘COST’ = [[1 2 3],
[6 5 6],
[3 8 9]]
Output: 11
A possible assignment is:
Assign task 1 to ninja 3, task 2 to ninja 1 and task 3 to ninja 2, total cost = 3 + 2 + 6 = 11
The first line will contain the integer 'T', the number of test cases. For each test case
The first line of each test case an integer ‘N’ denoting the number of Ninjas and tasks.
The next ‘N’ lines contain ‘N’ integers denoting the cost of ith Ninja completing the jth task.
For each test case, print a single integer denoting the minimum cost of the assignment.
You don't need to print anything. It has already been taken care of. Just implement the given function.
1 <= 'T' <= 10
1 <= 'N' <= 18
1 <= 'COST[i][j]' <= 10^5
Time Limit: 1 sec
We can generate all permutations of task assignments and for each assignment calculate the cost.
Find the minimum overall (N!) assignments
Algorithm :
Approach:
answer(K + 1, mask| (1<<i) = min ( answer(K + 1, mask | (1<<i) , answer(K, mask) + cost[k][I]
One thing to note here is that ‘K’ is always equal to the number set bits in ‘mask’, so we can remove that. So the ‘DP’ state now is just (mask), and if we have the answer(mask), then
answer(mask | (1<<i )) = min( answer(mask | (1<<i)), answer(mask) + cost[x][i] )
Here X = the number of set bits in ‘mask’.
Algorithm :