/* I will wait for you*/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <string>
#define make make_pair
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
const int maxn = 210;
const int maxm = 2000010;
const int maxs = 26;
const int inf = 0x3c3c3c3c;
const int P = 1000000007;
const double error = 1e-9;
inline int read()
{
int x = 0, f = 1;
char ch = getchar();
while (ch <= 47 || ch >= 58)
f = (ch == 45 ? -1 : 1), ch = getchar();
while (ch >= 48 && ch <= 57)
x = x * 10 + ch - 48, ch = getchar();
return x * f;
}
struct point
{
ll x, y;
} ans;
point operator - (point a, point b)
{
return (point) {a.x - b.x, a.y - b.y};
}
ll operator * (point a, point b)
{
return a.x * b.y - a.y * b.x;
}
bool operator < (point a, point b)
{
return a.x * a.y < b.x * b.y;
}
struct edge
{
int v, a, w, next;
} e[maxm];
int n, cnt, s, t, head[maxn], q[maxn], dis[maxn], vis[maxn],
w[maxn][maxn], x[maxn][maxn], y[maxn][maxn];
void insert(int u, int v, int a, int w)
{
e[cnt] = (edge) {v, a, w, head[u]}, head[u] = cnt++;
e[cnt] = (edge) {u, 0, -w, head[v]}, head[v] = cnt++;
}
int spfa()
{
memset(dis, 0x3c, sizeof dis);
memset(vis, 0, sizeof vis);
int a = 0, b = 1;
q[0] = t, dis[t] = 0, vis[t] = 1;
while (a != b) {
int u = q[a++];
vis[u] = 0, a %= maxn;
for (int i = head[u]; i != -1; i = e[i].next) {
int v = e[i].v;
if (e[i ^ 1].a && dis[v] > dis[u] - e[i].w) {
dis[v] = dis[u] - e[i].w;
if (!vis[v])
vis[v] = 1, q[b++] = v, b %= maxn;
}
}
}
return dis[s] < inf;
}
int dfs(int u, int a)
{
vis[u] = 1;
if (u == t)
return a;
int f, flow = 0;
for (int i = head[u]; a && i != -1; i = e[i].next) {
int v = e[i].v;
if (!vis[v] && e[i].a && dis[u] == dis[v] + e[i].w) {
f = dfs(v, min(a, e[i].a));
e[i].a -= f, e[i ^ 1].a += f;
a -= f, flow += f;
}
}
return flow;
}
void dinic()
{
while (spfa()) {
vis[t] = 1;
while (vis[t]) {
memset(vis, 0, sizeof vis);
dfs(s, inf);
}
}
}
point mincost()
{
memset(head, -1, sizeof head);
cnt = 0;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n ;j++)
insert(i, n + j, 1, w[i][j]);
for (int i = 1; i <= n; i++) {
insert(s, i, 1, 0);
insert(n + i, t, 1, 0);
}
dinic();
point tmp = {0, 0};
for (int i = 0; i < cnt; i += 2) {
int u = e[i ^ 1].v, v = e[i].v;
if (u != s && v != t && e[i].a == 0)
tmp.x += x[u][v - n], tmp.y += y[u][v - n];
}
if (tmp < ans)
ans = tmp;
return tmp;
}
void solve(point a, point b)
{
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
w[i][j] = (b.x - a.x) * y[i][j] + (a.y - b.y) * x[i][j];
point c = mincost();
if ((a - c) * (b - c) < 0)
solve(a, c), solve(c, b);
}
int main()
{
int T = read();
while (T--) {
n = read();
s = 2 * n + 1, t = 2 * n + 2;
ans = (point) {inf, inf};
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
x[i][j] = read();
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
y[i][j] = read();
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
w[i][j] = x[i][j];
point a = mincost();
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
w[i][j] = y[i][j];
point b = mincost();
solve(a, b);
printf("%lld\n", ans.x * ans.y);
}
return 0;
}
BZOJ3571【最小乘积最大匹配】
最新推荐文章于 2020-11-27 13:30:57 发布