Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- #define rep(i, a, b) for(int i = a; i < (b); ++i)
- #define all(x) begin(x), end(x)
- #define sz(x) (int)(x).size()
- typedef long long ll;
- typedef pair<int, int> pii;
- typedef vector<int> vi;
- const ll INF = 1e18;
- template <typename T> pair<T, vector<int>>
- weighted_matching(vector<vector<T>> &C) {
- int i = sz(C), m = sz(C[0]), c, j, s, r;
- vector<T> dist(m), potential(m);
- vi row_match(i), col_match(m, -1), cols(m), prev(m);
- T d, nd, cost = 0;
- for (; i--;) {
- rep(c, 0, m) dist[c] = C[i][c], cols[c] = c, prev[c] = i;
- for (s = 0;;) {
- for (j = s; j < m; j++) if (c = cols[j],
- nd = dist[c] - potential[c], j == s || d > nd)
- d = nd, swap(cols[s], cols[j]);
- if (!~(r = col_match[c = cols[s++]])) break;
- rep(j, 0, m) if (dist[j] > (nd = C[r][j] - C[r][c] +
- dist[c])) dist[j] = nd, prev[j] = r;
- }
- for (cost += dist[c]; s--;) j = cols[s],
- potential[j] = dist[j] - d;
- for (; r != i; swap(c, row_match[r]))
- r = col_match[c] = prev[c];
- }
- return {cost, row_match};
- }
- int main() {
- cin.tie(0)->sync_with_stdio(0);
- cin.exceptions(cin.failbit);
- int n, m;
- cin >> n >> m;
- vector<pii> bottles(n);
- rep(i, 0, n) cin >> bottles[i].first >> bottles[i].second;
- vector<pii> couriers(m);
- rep(i, 0, m) cin >> couriers[i].first >> couriers[i].second;
- pii goal;
- cin >> goal.first >> goal.second;
- ll ans = 0;
- rep(i, 0, n) {
- ans += abs(goal.first - bottles[i].first) + abs(goal.second - bottles[i].second);
- }
- vector<vi> cost(n, vi(n - 1 + m));
- rep(i, 0, n) {
- rep(j, 0, m) {
- cost[i][j] = abs(couriers[j].first - bottles[i].first) + abs(couriers[j].second - bottles[i].second);
- }
- rep(j, 0, n - 1) cost[i][j + m] = abs(goal.first - bottles[i].first) + abs(goal.second - bottles[i].second);
- }
- ans += weighted_matching(cost).first;
- cout << ans;
- }
Advertisement
Add Comment
Please, Sign In to add comment