Description

题库链接

\(T\) 组询问,每组询问给你个 \(2\times N\) 的带权二分图,两个权值 \(a,b\) ,让你做匹配使得 \[\sum a\times \sum b\] 最小。

\(N\leq 70,T\leq 3\)

Solution

容易发现就是 [COGS 2401]Time is Money 的变种。

我们用相同的思路,只是将 \(kruskal\) 求最小生成树的过程改为 \(KM\) 求最佳匹配。由于 \(KM\) 是求匹配的最大值,我们只需要将权值取相反数即可。

Code

//It is made by Awson on 2018.3.8
#include <bits/stdc++.h>
#define LL long long
#define dob complex<double>
#define Abs(a) ((a) < 0 ? (-(a)) : (a))
#define Max(a, b) ((a) > (b) ? (a) : (b))
#define Min(a, b) ((a) < (b) ? (a) : (b))
#define Swap(a, b) ((a) ^= (b), (b) ^= (a), (a) ^= (b))
#define writeln(x) (write(x), putchar('\n'))
#define lowbit(x) ((x)&(-(x)))
using namespace std;
const int N = 70, INF = 1e9;
void read(int &x) {
char ch; bool flag = 0;
for (ch = getchar(); !isdigit(ch) && ((flag |= (ch == '-')) || 1); ch = getchar());
for (x = 0; isdigit(ch); x = (x<<1)+(x<<3)+ch-48, ch = getchar());
x *= 1-2*flag;
}
void print(int x) {if (x > 9) print(x/10); putchar(x%10+48); }
void write(int x) {if (x < 0) putchar('-'); print(Abs(x)); } struct node {
int x, y;
node (int _x = 0, int _y = 0) {x = _x, y = _y; }
int operator * (const node &b) const {return x*b.y-y*b.x; }
node operator + (const node &b) const {return node(x+b.x, y+b.y); }
node operator - (const node &b) const {return node(x-b.x, y-b.y); }
void print() {writeln(x*y); }
}ans, A, B;
int n, a[N+5][N+5], b[N+5][N+5], c[N+5][N+5];
int vis1[N+5], vis2[N+5], E1[N+5], E2[N+5], sla[N+5], match[N+5]; bool dfs(int u) {
vis1[u] = 1;
for (int i = 1; i <= n; i++)
if (vis2[i] == 0) {
int tmp = E1[u]+E2[i]-c[u][i];
if (tmp == 0) {
vis2[i] = 1;
if (match[i] == 0 || dfs(match[i])) {
match[i] = u; return true;
}
}else sla[i] = Min(sla[i], tmp);
}
return false;
}
node KM() {
for (int i = 1; i <= n; i++) {
E1[i] = -INF; E2[i] = match[i] = 0;
for (int j = 1; j <= n; j++) E1[i] = Max(E1[i], c[i][j]);
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) sla[j] = INF;
while (1) {
for (int j = 1; j <= n; j++) vis1[j] = vis2[j] = 0;
if (dfs(i)) break; int tmp = INF;
for (int j = 1; j <= n; j++) if (vis2[j] == 0) tmp = Min(tmp, sla[j]);
for (int j = 1; j <= n; j++) {
if (vis1[j]) E1[j] -= tmp;
if (vis2[j]) E2[j] += tmp; else sla[j] -= tmp;
}
}
}
node ans;
for (int i = 1; i <= n; i++) ans = ans+node(a[match[i]][i], b[match[i]][i]);
return ans;
}
void update(node &ans, node x) {if (x.x*x.y < ans.x*ans.y) ans = x; }
bool judge(node A, node B, node C) {return (B-A)*(C-A) >= 0; }
void doit(node A, node B) {
int x = A.x-B.x, y = B.y-A.y; node C;
for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) c[i][j] = a[i][j]*y+b[i][j]*x;
update(ans, C = KM());
if (judge(A, B, C)) return;
doit(A, C); doit(C, B);
}
void work() {
read(n);
for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) read(a[i][j]);
for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) read(b[i][j]);
for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) c[i][j] = -a[i][j];
A = ans = KM();
for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) c[i][j] = -b[i][j];
update(ans, B = KM());
doit(A, B); ans.print();
}
int main() {
int t; read(t); while (t--) work(); return 0;
}

最新文章

  1. The Installation and Compilation of OpenCASCADE
  2. 根据UUID和MD5, 生成可以用作Token的字符串
  3. BZOJ 1461: 字符串的匹配
  4. 很好的一篇讲LTP在编解码中的作用的文章
  5. iostat命令简单说说
  6. hi3531播放1080p60f, 延迟越来越大的问题与解决办法
  7. android学习日记04--开发中的通用细节
  8. [Audio processing] wav音频文件合并
  9. Struts2 + uploadify 多文件上传完整的例子!
  10. Lambda表达式的几种使用方式
  11. Silverlight中如何自己写方法将DataTable转换为PagedCollectionView数据(动态创建类)
  12. PYTHON的前端部分
  13. Ubuntu 14.04 LTS 初装成
  14. PostgreSQL字段类型说明
  15. jquery ----&gt; How to Create a Basic Plugin (翻译)
  16. springboot 解决 woff2、ttf 跨域无法解析问题
  17. python 经验:把全局变量放在一个类中
  18. Linux 基础教程 37-进程命令
  19. 层层递进Struts1(五)之处理流程
  20. 【LG3233】[HNOI2014]世界树

热门文章

  1. python爬虫遇到状态码304,705
  2. 【Spring系列】Spring mvc整合druid
  3. transient 与 volatile 笔记
  4. 详谈C++虚函数表那回事(多重继承关系)
  5. Django 模版语法
  6. SQL的介绍及MySQL的安装
  7. 顺企网 爬取16W数据保存到Mongodb
  8. Linux安装mongodb总结
  9. VS 2008 开发WinCE程序 编译部署速度慢的解决办法
  10. ELK学习总结(3-2)elk的过滤查询