Minimum-Cost Task Assignment (Large)
- Memory limit
- 256 MB
- Correctness time limit
- 10000 ms
- Solvers
- 0
N employees must each take exactly one of N tasks, and every task must go to exactly one employee. Assigning
task j to employee i costs cost[i][j]. Return the minimum total cost.
Examples
| cost | result |
|---|---|
[[4, 1, 3], [2, 0, 5], [3, 2, 2]] | 5 |
[[7]] | 7 |
[[1, 2], [3, 6]] | 5 |
Example 1 — 1 + 2 + 2; taking the cheapest cell cost[1][1] = 0 forces a total of at least 6.
Example 2 — one employee, one task.
Example 3 — greedily taking cost[0][0] = 1 gives 7, but 2 + 3 = 5.
Efficiency
This problem has performance tests. Judge the required time complexity from the input sizes in the constraints.
Function signature
solution(cost: int[][]) → int
Constraints
-
cost.length, everycost[i].length -
cost[i][j]
Expected complexity
- Time
- O(N^3)
- Space
- O(N)
Examples
| cost | Expected |
|---|---|
[[4,1,3],[2,0,5],[3,2,2]] | 5 |
[[7]] | 7 |
[[1,2],[3,6]] | 5 |
Hint
Hint
This is the assignment problem; use the Hungarian algorithm with potentials u[i] + v[j] <= cost[i][j]. Add employees one
at a time: grow an alternating tree over tight cells, and when stuck raise u on tree rows and lower v on tree columns by
the minimum slack delta, until a free task is reached and the alternating path is flipped. Maintaining the per-column minimum
slack minv[j] incrementally gives per employee and overall; recomputing delta from scratch every step is
and exceeds the time limit.
Time limits per language
| Language | Efficiency time limit | |
|---|---|---|
| C (GCC, gnu17) | 200 ms | estimated |
| C++ (GCC, gnu++23) | 200 ms | derived |
| Python 3.13 | 3226 ms | derived |
| Java 25 | 1070 ms | derived |
| Kotlin 2.4 (JVM) | 1400 ms | estimated |
| C# 14 (.NET 10) | 1400 ms | estimated |
Test groups
| Group | Weight | Tests | Visibility |
|---|---|---|---|
| sample | 0 | 3 | inputs and outputs |
| correctness | 40 | 11 | verdict only |
| performance | 60 | 5 | verdict only |