题面

题解

这又是一种套路啊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. python-面向过程编程

    面向过程: 核心是过程.过程指的是解决问题的步骤,设计一条流水线,机械式的思维方式. 优点:复杂的问题流程化,进而简单化

  2. MVC中使用EF的技巧集(一)

    一.建好数据库后,向项目中添加数据模型. 1.右键点击“Models” 文件夹,选择“添加”,再选择“添加新项”. 2.在“添加新项”窗口,选择左边的“数据”,然后再在右边选择“ADO.NET 实体数 ...

  3. playfair

    又是一道实验吧的题,哈哈,我比较弱. 因为题目写了play我首先想到的是playfair,好,下面先看下百科 好了,已知了密钥: 所以有: s n f m th b g o ui c j p vy d ...

  4. December 04th 2016 Week 50th Sunday

    Learn wisdom by the follies of others. 前车之鉴,后人之师. Maybe my personal state is that others can learn w ...

  5. upper_bound()与lower_bound()的使用

    upper_bound()与lower_bound()的使用 c++中的许多库函数可以使我们的代码量大大减少,也可使问题简单化.很早之前就接触了upper_bound()与lower_bound(), ...

  6. UE4的csv文件导入、URL地址的读取及动态材质的设置

    1.csv文件的导入 UE4是可以直接导入csv文件的,其过程和其他文件资源(图片Texture,静态网格物体StaticMesh等)相似,但在导入过程中有一些需要注意的点. 如下图所示 这是一份编辑 ...

  7. JNLP应用程序无法打开的解决办法

    JNLP应用程序无法打开: 1.控制面板-Java-Java 选项卡-查看.用户选项卡勾选对应版本JDK(没有就添加,路径填类似:D:\Program Files\Java\jre6\bin\java ...

  8. jupyter notebook设置主题背景,字体和扩展插件

    windows上安装Anaconda (IPython notebook) Anaconda是一个包与环境的管理器,一个Python发行版,以及一个超过1000多个开源包的集合.它是免费和易于安装的, ...

  9. PHP中include和require

    1.include语句 使用include语句可以告诉PHP提取特定的文件,并载入它的全部内容 1 <?php 2 inlude "fileinfo.php"; 3 4 // ...

  10. BZOJ1444:[JSOI2009]有趣的游戏(AC自动机,矩阵乘法)

    Description Input 注意 是0<=P, n , l, m≤ 10. Output Sample Input input 1 3 2 2 1 2 1 2 AB BA AA inpu ...