Content deleted Content added
fix link to Strongly-polynomial time |
|||
Line 149:
#include <cassert>
import std;
using
using Vector = std::vector;
/**
Line 158 ⟶ 159:
* @return true if b < a
*/
template <
bool ckmin(T return b < a ? a = b, 1 : 0; } /**
Line 172 ⟶ 176:
* to assign the first (j+1) jobs to distinct workers
*/
template <typename T>
const int J =
const int W = static_cast<int>(C[0].size());
assert(J <= W);
// job[w] = job assigned to w-th worker, or -1 if no job assigned
// note: a W-th worker was added for convenience
Vector<T> yt(W + 1); // potentials // -yt[W] will equal the sum of all deltas
const T inf = std::numeric_limits<T>::max();
for (int
int
job[
// min reduced cost over edges from Z to worker w
while (job[
in_Z[
const int j = job[
T delta = inf;
int
for (int w = 0; w < W; ++w) {
if (!
if (ckmin(
prv[w] =
if (ckmin(delta,
}
}
// delta will always be nonnegative,
// except possibly during the first time this loop runs
// if any entries of C[
for (int w = 0; w <= W; ++w) {
if (
yt[w] -= delta;
} else {
minTo[w] -= delta;
}▼
}
}
// update assignments along alternating path
for (int w;
job[ answers.push_back(-yt[W]);
}
Line 229 ⟶ 241:
* wash windows: Bob -> 3
*/
void
assert((hungarian(costs) == vector<int>{5, 9, 15}));
}
// solves https://open.kattis.com/problems/cordonbleu
void
int N
Vector<Pair<int, int>> C(M);
for (auto &b : bottles) cin >> b.first >> b.second;▼
Vector<Pair<int, int>> bottles(N);
for (auto &c : couriers) cin >> c.first >> c.second;▼
for (const Pair<int, int>& b : bottles)
vector<vector<int>> costs(N, vector<int>(N + M - 1));▼
Pair<int, int> rest;
auto dist = [&](const Pair<int, int>& x, const Pair<int, int>& y) -> int {
return std::abs(x.first - y.first) + std::abs(x.second - y.second);
};
for (int b = 0; b < N; ++b) {
for (int c = 0; c < M; ++c)
costs[b][c] = dist(couriers[c], bottles[b]) + dist(bottles[b], rest);
▲ }
▲ for (int _ = 0; _ < N - 1; ++_) { // restaurant -> bottle -> restaurant
costs[b][_ + M] = 2 * dist(bottles[b], rest);
}
}
int main() {
}
</syntaxhighlight>
|