[HNOI 2014]画框

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;
}

转载于:https://www.cnblogs.com/NaVi-Awson/p/8529148.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值