zoukankan      html  css  js  c++  java
  • 【HNOI2014】画框

    题面

    题解

    这又是一种套路啊233

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

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

    (overrightarrow{mathrm{AB}} imes overrightarrow{mathrm{AC}})最小

    [egin{aligned} ecause overrightarrow{mathrm{AB}} imes 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) imes y_mathrm C + (y_mathrm A - y_mathrm B) imes 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) imes a[i][j] + (x_mathrm B - x_mathrm A) imes 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
    ", ans);
    	}
    	return 0;
    }
    
  • 相关阅读:
    《人月神话》读书笔记二
    正则表达式进阶篇
    Zend Framework学习之自定义认证适配器
    Zend Framework学习之常用校验器
    Zend Framework学习之Zend_Mail
    《人月神话》读书笔记一
    Zend Framework学习之校验器Zend_Validate
    我的第一个C++程序
    Zend Framework学习之Zend_Filter_StripTags
    《黑客与画家》读书笔记三(完结)
  • 原文地址:https://www.cnblogs.com/cj-xxz/p/10395705.html
Copyright © 2011-2022 走看看