Content deleted Content added
add more about the time complexity in →Adding the j-th job in O(jW) time |
add citation for code |
||
Line 126:
===Implementation in C++===
For convenience of implementation, the code below adds an additional worker <math>w_{W}</math> such that <math>y(w_{W})</math> stores the negation of the sum of all <math>\Delta</math> computed so far. After the <math>j</math>th job is added and the matching updated, the cost of the current matching equals the sum of all <math>\Delta</math> computed so far, or <math>-y(w_{W})</math>.
This code is adapted from e-maxx :: algo.<ref>{{cite web
| url = http://e-maxx.ru/algo/assignment_hungary#6
| title = Hungarian Algorithm for Solving the Assignment Problem
| author = <!--Not stated-->
| date = August 23, 2012
| website = e-maxx :: algo
| access-date = May 13, 2023
}}</ref>
<syntaxhighlight lang="cpp" line="1">
/**
* Solution to https://open.kattis.com/problems/cordonbleu using Hungarian
* algorithm.
*/
Line 170 ⟶ 181:
int w_cur = W;
job[w_cur] = j_cur;
//
vector<T>
vector<int> prv(W + 1, -1); //
vector<bool>
while (job[w_cur] != -1) { // runs at most j_cur + 1 times
const int j = job[w_cur];
T delta = inf;
int w_next;
for (int w = 0; w < W; ++w) {
if (!
if (ckmin(
if (ckmin(delta, min_to[w])) w_next = w;
}
}
Line 189 ⟶ 201:
// negative
for (int w = 0; w <= W; ++w) {
if (
else
}
w_cur = w_next;
|