[AGC034D] Manhattan Max Matching

Posted top_secret的小尛博客

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[AGC034D] Manhattan Max Matching相关的知识,希望对你有一定的参考价值。

传送门

解答

明显用网络流……但是图太大了!而且曼哈顿距离暴难处理!!

……然后想到拆点:\\((x_1, y_1), (x_2, y_2)\\)之间的曼哈顿距离

\\[\\begin{aligned} dist &= |x_1-x_2|+|y_1-y_2| \\\\ &= \\max\\{x_1-x_2, x_2-x_1\\}+\\max\\{y_1-y_2, y_2-y_1\\} \\\\ &= \\max\\{(x_1+y_1)+(-x_2-y_2), \\cdots, (-x_1-y_1)+(x_2+y_2)\\} \\end{aligned} \\]

之后,就只有在整体上取\\(\\max\\),随便建图就可以了。

代码

#include <bits/stdc++.h>
#define int long long
using namespace std;

const int inf = 10, maxn = 1e5;

int ans;

namespace MCMF{
	int head[maxn], nxt[maxn], to[maxn], c[maxn], r[maxn], tot = 1;
	void add(int u, int v, int w, int f) {
		assert(++tot < maxn);
		nxt[tot] = head[u];
		to[tot] = v;
		c[tot] = w;
		r[tot] = f;
		head[u] = tot;
	}
	void add2(int u, int v, int w, int f) {
		add(u,v,w,f), add(v,u,-w,0);
	}
#define mp make_pair
	typedef pair<long long, int> qnode;
	int dis[maxn], inq[maxn], pre[maxn];
	void spfa(int s) {
		memset(dis, 0x3f, sizeof(dis));
		memset(pre, 0, sizeof(pre));
		queue<int> q;
		dis[s] = 0;
		q.push(s);
		while (!q.empty()) {
			int u = q.front();
			q.pop(); inq[u] = 0;
			for (int e = head[u]; e; e = nxt[e]) {
				if (!r[e]) continue;
				int v = to[e], w = c[e];
				if (dis[u]+w < dis[v]) {
					pre[v] = e;
					dis[v] = dis[u]+w;
					if (!inq[v]) {
						q.push(v);
						inq[v] = 1;
					}
				}
			}
		}
	}
	void augment(int t) {
		int flow = inf;
		for (int e = pre[t]; e; e = pre[to[e^1]]) {
			flow = min(flow, r[e]);
		}
		for (int e = pre[t]; e; e = pre[to[e^1]]) {
			r[e] -= flow;
			r[e^1] += flow;
		}
		ans += flow*dis[t];
	}
}
using namespace MCMF;


int n, rx[maxn], ry[maxn], rc[maxn], bx[maxn], by[maxn], bc[maxn];
signed main() {
	cin >> n;
	for (int i = 0; i < n; ++i) {
		cin >> rx[i] >> ry[i] >> rc[i];
		add2(1, i+10, 0, rc[i]);
		add2(i+10, 3, rx[i]+ry[i], inf);
		add2(i+10, 4, rx[i]-ry[i], inf);
		add2(i+10, 5, -rx[i]+ry[i], inf);
		add2(i+10, 6, -rx[i]-ry[i], inf);
	}
	for (int i = n; i < 2*n; ++i) {
		cin >> bx[i] >> by[i] >> bc[i];
		add2(3, i+10, -bx[i]-by[i], inf);
		add2(4, i+10, -bx[i]+by[i], inf);
		add2(5, i+10, bx[i]-by[i], inf);
		add2(6, i+10, bx[i]+by[i], inf);
		add2(i+10, 2, 0, bc[i]);
	}
	while (spfa(1), pre[2]) augment(2);
	cout << -ans;
}

以上是关于[AGC034D] Manhattan Max Matching的主要内容,如果未能解决你的问题,请参考以下文章

cf(M) Manhattan Mornings

POJ3133 Manhattan Wiring

Poj3133Manhattan Wiring (插头DP)

AGC 053 C Random Card Game 题解

[AGC055C]Weird LIS

agc040B - Two Contests