题面

题解

这又是一种套路啊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;
}

【HNOI2014】画框的更多相关文章

  1. 【LG3236】[HNOI2014]画框

    [LG3236][HNOI2014]画框 题面 洛谷 题解 和这题一模一样. 将最小生成树换成\(KM\)即可. 关于复杂度,因为决策点肯定在凸包上,且\(n\)凸包的期望点数为\(\sqrt {\l ...

  2. bzoj 3571: [Hnoi2014]画框

    Description 小T准备在家里摆放几幅画,为此他买来了N幅画和N个画框.为了体现他的品味,小T希望能合理地搭配画与画框,使得其显得既不过于平庸也不太违和.对于第 幅画与第 个画框的配对,小T都 ...

  3. [HNOI2014]画框

    题目描述 小T准备在家里摆放几幅画,为此他买来了N幅画和N个画框.为了体现他的品味,小T希望能合理地搭配画与画框,使得其显得既不过于平庸也不太违和. 对于第 幅画与第 个画框的配对,小T都给出了这个配 ...

  4. BZOJ3571 & 洛谷3236:[HNOI2014]画框——题解

    https://www.lydsy.com/JudgeOnline/problem.php?id=3571 https://www.luogu.org/problemnew/show/P3236 小T ...

  5. BZOJ3571 : [Hnoi2014]画框

    题目是要求最小乘积最小权匹配, 将一种方案看做一个二维点(x,y),x=a值的和,y=b值的和,所有方案中只有在下凸壳上的点才有可能成为最优解 首先要求出两端的方案l,r两个点 l就是a值的和最小的方 ...

  6. bzoj3571: [Hnoi2014]画框 最小乘积匹配+最小乘积XX总结,

    思路大概同bzoj2395(传送门:http://www.cnblogs.com/DUXT/p/5739864.html),还是将每一种匹配方案的Σai看成x,Σbi看成y,然后将每种方案转化为平面上 ...

  7. luogu P3236 [HNOI2014]画框

    传送门 我们把一种方案的\(\sum a_{i,j}\)和\(\sum b_{i,j}\)看成点\((\sum a_{i,j},\sum b_{i,j})\),那么就只要求横纵坐标之积最小的点,类似于 ...

  8. 【bzoj3751】 Hnoi2014—画框

    http://www.lydsy.com/JudgeOnline/problem.php?id=3571 (题目链接) 题意 给出一个$2*N$个点的二分图,$N*N$条边,连接$i$和$j$的边有两 ...

  9. BZOJ 3571 [Hnoi2014]画框(最小乘积完美匹配)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3571 [题目大意] 给出一张二分图,每条边上有a,b两个值,求完美匹配, 使得suma ...

  10. 洛谷P3236 [HNOI2014]画框(最小乘积KM)

    题面 传送门 题解 我似乎连\(KM\)都不会打啊→_→ 和bzoj2395是一样的,只不过把最小生成树换成\(KM\)了.因为\(KM\)跑的是最大权值所以取个反就行了 //minamoto #in ...

随机推荐

  1. TreeView控件概述、属性与方法

    1.作用:用于显示Node结点的分层列表.2.添加到控件箱菜单命令:工程 | 部件,在部件对话框中选择:Microsoft Windows Common Controls 6.03.TreeView控 ...

  2. Oracle诊断工具 - ORA-2730x Troubleshooting Tool

    通常情况下,ORA-27300 ORA-27301 ORA-27302错误的原因是操作系统的系统调用错误或者操作系统配置问题,错误格式:ORA-27300: OS system dependent o ...

  3. UIWebView如何加载本地图片

    UIWebView如何加载本地图片 UIWebView加载本地图片是有实用价值的.比方说,有时候我们需要本地加载静态页来显示相关帮助信息,而这些帮助信息当中含有很多很多的富文本,用代码实现难度较大,这 ...

  4. 使用MVC Razor生成格式良好的HTML Body作为邮件内容

    PS: 实例化邮件类MailMessage有一个属性IsBodyHtml指示邮件正文是否是HTML格式. 我想利用Razor View的Model Binding / Rendering功能为我从AS ...

  5. [EffectiveC++]item43:学习处理模板化基类内的名称

  6. CSS控制列表与导航的制作

    <style type="text/css"> /*body默认是有边距的*/ body{ margin:0;} /*ul默认是有边距的所以先将边距去掉IE78只要加上 ...

  7. Javascript能做什么 不能做什么。

    JavaScript可以做什么?用JavaScript可以做很多事情,使网页更具交互性,给站点的用户提供更好,更令人兴奋的体验. JavaScript使你可以创建活跃的用户界面,当用户在页面间导航时向 ...

  8. Mysql索引详解及优化(key和index区别)

    MySQL索引的概念    索引是一种特殊的文件(InnoDB数据表上的索引是表空间的一个组成部分),它们包含着对数据表里所有记录的引用指针.更通俗的说,数据库索引好比是一本书前面的目录,能加快数据库 ...

  9. base大家族详解

    base大家族详解 */--> pre.src {background-color: #292b2e; color: #b2b2b2;} pre.src {background-color: # ...

  10. openfalcon-0.2 配置

    hbs 配置文件 { "debug": true, "database": "openfalcon:123456@tcp(172.16.230.188 ...