【HNOI2014】画框

题面

题解

这又是一种套路啊233

\(\sum a_i\)\(\sum b_i\)分别看做\(x\)\(y\),投射到平面直角坐标系中,于是就是找\(xy\)最小的点

于是可以先找出\(x\)最小的点\(\mathrm{A}\)\(y\)最小的点\(\mathrm{B}\),然后找到在\(\mathrm{AB}\)下方的最远的点\(\mathrm{C}\)

\(\overrightarrow{\mathrm{AB}} \times \overrightarrow{\mathrm{AC}}\)最小

\[\begin{aligned} \because \overrightarrow{\mathrm{AB}} \times \overrightarrow{\mathrm{AC}} &= (x_{\mathrm{B}} - x_{\mathrm{A}})(y_{\mathrm{C}} - y_{\mathrm{A}}) - (y_{\mathrm{B}} - y_{\mathrm{A}})(x_\mathrm{C} - x_\mathrm{A}) \\ &= (x_\mathrm B - x_\mathrm A) \times y_\mathrm C + (y_\mathrm A - y_\mathrm B) \times x_\mathrm C + y_\mathrm B x_\mathrm A - x_\mathrm B y_\mathrm A \end{aligned} \]

于是将权值改成\(\mathrm{g}[i][j] = (y_\mathrm A - y_\mathrm B) \times a[i][j] + (x_\mathrm B - x_\mathrm A)\times b[i][j]\),然后用\(\mathrm{KM}\)找出\(\mathrm C\)

找到\(\mathrm C\)之后用叉积判断一下\(\mathrm C\)是不是在\(\mathrm{AB}\)的下方,如果是的话,就递归处理\(\mathrm{AC, CB}\)

复杂度\(\mathrm{O}(\)能过\()\)

代码

#include<cstdio>
#include<cstring>
#include<cctype>
#include<algorithm>
#include<climits>
#define RG register
#define file(x) freopen(#x".in", "r", stdin), freopen(#x".out", "w", stdout)
#define clear(x, y) memset(x, y, sizeof(x))

inline int read()
{
	int data = 0, w = 1; char ch = getchar();
	while(ch != '-' && (!isdigit(ch))) ch = getchar();
	if(ch == '-') w = -1, ch = getchar();
	while(isdigit(ch)) data = data * 10 + (ch ^ 48), ch = getchar();
	return data * w;
}

const int N(75);
int T, n, a[N][N], b[N][N], ans;
struct vector { int x, y; };
inline vector operator - (const vector &lhs, const vector &rhs)
	{ return (vector) {lhs.x - rhs.x, lhs.y - rhs.y}; }
inline int operator * (const vector &lhs, const vector &rhs)
	{ return lhs.x * rhs.y - lhs.y * rhs.x; }
int g[N][N], lx[N], ly[N], visx[N], visy[N], match[N];
bool hungary(int x)
{
	visx[x] = 1;
	for(RG int to = 1; to <= n; to++)
		if(!visy[to] && lx[x] + ly[to] == g[x][to])
		{
			visy[to] = true;
			if(!match[to] || hungary(match[to])) return (match[to] = x, true);
		}
	return false;
}

void build(int valx, int valy)
{
	for(RG int i = 1; i <= n; i++) for(RG int j = 1; j <= n; j++)
		g[i][j] = -(valx * a[i][j] + valy * b[i][j]);
}

vector KM()
{
	for(RG int i = 1; i <= n; i++)
		ly[i] = 0, lx[i] = *std::max_element(g[i] + 1, g[i] + n + 1);
	memset(match, 0, sizeof match);
	for(RG int x = 1; x <= n; x++)
		while(1)
		{
			clear(visx, 0), clear(visy, 0);
			if(hungary(x)) break;
			int inc = INT_MAX;
			for(RG int i = 1; i <= n; i++) if(visx[i])
				for(RG int j = 1; j <= n; j++) if(!visy[j])
					inc = std::min(inc, lx[i] + ly[j] - g[i][j]);
			for(RG int i = 1; i <= n; i++) if(visx[i]) lx[i] -= inc;
			for(RG int i = 1; i <= n; i++) if(visy[i]) ly[i] += inc;
		}
	vector ans = (vector) {0, 0};
	for(RG int i = 1; i <= n; i++)
		ans.x += a[match[i]][i], ans.y += b[match[i]][i];
	return ans;
}

void solve(const vector &A, const vector &B)
{
	build(A.y - B.y, B.x - A.x);
	vector C = KM(); ans = std::min(ans, C.x * C.y);
	if((B - A) * (C - A) >= 0) return;
	solve(A, C), solve(C, B);
}

int main()
{
	T = read();
	while(T--)
	{
		n = read();
		for(RG int i = 1; i <= n; i++)
			for(RG int j = 1; j <= n; j++)
				a[i][j] = read();
		for(RG int i = 1; i <= n; i++)
			for(RG int j = 1; j <= n; j++)
				b[i][j] = read();
		build(1, 0); vector A = KM();
		build(0, 1); vector B = KM();
		ans = std::min(A.x * A.y, B.x * B.y);
		solve(A, B); printf("%d\n", ans);
	}
	return 0;
}
posted @ 2019-02-18 15:04  xgzc  阅读(184)  评论(0编辑  收藏  举报