bzoj3571 [Hnoi2014]画框

Description

小T准备在家里摆放几幅画,为此他买来了N幅画和N个画框。为了体现他的品味,小T希望能合理地搭配画与画框,使得其显得既不过于平庸也不太违和。对于第i幅画与第j个画框的配对,小T都给出了这个配对的平凡度Aij 与违和度Bij 。整个搭配方案的总体不和谐度为每对画与画框平凡度之和与每对画与画框违和度的乘积。具体来说,设搭配方案中第i幅画与第Pi个画框配对,则总体不和谐度为

小T希望知道通过搭配能得到的最小的总体不和谐度是多少。

Input

输入文件第 行是一个正整数T ,表示数据组数,接下来是T组数据。
对于每组数据,第 行是一个正整数N,表示有N对画和画框。
第2到第N+1行,每行有N个非负整数,第i+1 行第j个数表示Aij 。
第N+2到第2*N+1行,每行有N个非负整数,第i+N+1 行第j个数表示Bij 。

Output

包含T行,每行一个整数,表示最小的总体不和谐度

Sample Input

1
3
4 3 2
2 3 4
3 2 1
2 3 2
2 2 4
1 1 3

Sample Output

30

HINT 

第1幅画搭配第3个画框,第2幅画搭配第1个画框,第3 幅画搭配第2个画框,则总体不和谐度为30

N<=70,T<=3,Aij<=200,Bij<=200

 

正解:最小乘积$KM$。

学习了一下最小乘积生成树,突然意识到原来这是一道傻逼题,然后就把这题做了。。

这题跟最小乘积生成树几乎一模一样,只是把$kruskal$换成了$KM$而已。

我们考虑把所有解的$x$(也就是$a$)之和与$y$(也就是$b$)之和投影到笛卡尔坐标系上,那么每个解就对应一个点$(x,y)$。

我们要使$xy$最小,很显然,使得$xy$最小的点一定在反比例函数$xy=k$上,其中$k$也是最小的。

然后感性地发现,最优解一定在所有解组成的点的上凸包上。

那么我们就有一个递归的做法,首先求出只有$x$或$y$的最优解$A$和$B$,也就是直接跑$KM$,那么实际最优解一定在这两个点与原点之间的那个区域上。

然后我们可以找到离$AB$在原点方向上最远的点$C$,递归做$A,C$与$B,C$即可。

现在有一个问题,就是如何找$C$。

因为$C$离$AB$最远,所以我们可以得到$S\Delta ABC$最大的结论。

$\vec{AB}=(B.y-A.y,B.x-A.x)$,$\vec{AC}=(C.y-A.y,C.x-A.x)$,而这两个向量叉积的一半即为$S\Delta ABC$。

因为叉积是有向的,所以我们实际上要使$\vec{AB}*\vec{AC}$最小,也就是使得$(B.x-A.x)*(C.y-A.y)-(B.y-A.y)*(C.x-A.x)$最小。

那么把它拆开,我们可以得到$(B.x-A.x)*C.y+(A.y-B.y)*C.x-A.y*(B.x-A.x)+A.x*(B.y-A.y)$。

那么只有前面那一半是不定的,我们可以发现,只要把$g[i][j]$改成$(B.x-A.x)*y[i][j]+(A.y-B.y)*x[i][j]$,再跑一遍$KM$,就能找到$C$了。

然后我们递归就行了,终止条件就是叉积$>=0$。由于要求最小权匹配,我们把边权取反就行了。

 

  1 //It is made by wfj_2048~
  2 #include <algorithm>
  3 #include <iostream>
  4 #include <cstring>
  5 #include <cstdlib>
  6 #include <cstdio>
  7 #include <vector>
  8 #include <cmath>
  9 #include <queue>
 10 #include <stack>
 11 #include <map>
 12 #include <set>
 13 #define inf (1<<30)
 14 #define il inline
 15 #define RG register
 16 #define ll long long
 17 
 18 using namespace std;
 19 
 20 int visx[110],visy[110],lx[110],ly[110],lk[110],slack[110],cnt;
 21 int g[110][110],a[110][110],b[110][110],n;
 22 
 23 struct point{ int x,y; }ans,ansa,ansb;
 24 
 25 il point operator - (const point &A,const point &B){
 26     return (point){A.x-B.x,A.y-B.y};
 27 }
 28 
 29 il int cross(RG point A,RG point B){ return A.x*B.y-A.y*B.x; }
 30 
 31 il int gi(){
 32     RG int x=0,q=1; RG char ch=getchar();
 33     while ((ch<'0' || ch>'9') && ch!='-') ch=getchar();
 34     if (ch=='-') q=-1,ch=getchar();
 35     while (ch>='0' && ch<='9') x=x*10+ch-48,ch=getchar();
 36     return q*x;
 37 }
 38 
 39 il int dfs(RG int x){
 40     visx[x]=cnt;
 41     for (RG int y=1;y<=n;++y){
 42     if (visy[y]==cnt) continue;
 43     if (lx[x]+ly[y]==g[x][y]){
 44         visy[y]=cnt;
 45         if (!lk[y] || dfs(lk[y])){ lk[y]=x; return 1; }
 46     } else slack[y]=min(slack[y],lx[x]+ly[y]-g[x][y]);
 47     }
 48     return 0;
 49 }
 50 
 51 il point KM(){
 52     for (RG int i=1;i<=n;++i) lk[i]=ly[i]=0,lx[i]=-inf;
 53     for (RG int i=1;i<=n;++i)
 54     for (RG int j=1;j<=n;++j) lx[i]=max(lx[i],g[i][j]);
 55     for (RG int x=1;x<=n;++x){
 56     for (RG int i=1;i<=n;++i) slack[i]=inf;
 57     while (++cnt,!dfs(x)){
 58         RG int d=inf;
 59         for (RG int i=1;i<=n;++i)
 60         if (visy[i]!=cnt) d=min(d,slack[i]);
 61         for (RG int i=1;i<=n;++i){
 62         if (visx[i]==cnt) lx[i]-=d;
 63         if (visy[i]==cnt) ly[i]+=d; else slack[i]-=d;
 64         }
 65     }
 66     }
 67     RG point now=(point){0,0};
 68     for (RG int i=1;i<=n;++i)
 69     now.x+=a[lk[i]][i],now.y+=b[lk[i]][i];
 70     RG int Ans=ans.x*ans.y,Now=now.x*now.y;
 71     if (Ans>Now) ans=now; return now;
 72 }
 73 
 74 il void solve(RG point A,RG point B){
 75     for (RG int i=1;i<=n;++i)
 76     for (RG int j=1;j<=n;++j)
 77         g[i][j]=-(b[i][j]*(B.x-A.x)+a[i][j]*(A.y-B.y));
 78     RG point C=KM(); if (cross(B-A,C-A)>=0) return;
 79     solve(A,C),solve(C,B); return;
 80 }
 81 
 82 il void work(){
 83     n=gi(),ans.x=ans.y=40000;
 84     for (RG int i=1;i<=n;++i)
 85     for (RG int j=1;j<=n;++j) a[i][j]=gi();
 86     for (RG int i=1;i<=n;++i)
 87     for (RG int j=1;j<=n;++j) b[i][j]=gi();
 88     for (RG int i=1;i<=n;++i)
 89     for (RG int j=1;j<=n;++j) g[i][j]=-a[i][j];
 90     ansa=KM();
 91     for (RG int i=1;i<=n;++i)
 92     for (RG int j=1;j<=n;++j) g[i][j]=-b[i][j];
 93     ansb=KM();
 94     solve(ansa,ansb),printf("%d\n",ans.x*ans.y); return;
 95 }
 96 
 97 int main(){
 98 #ifndef ONLINE_JUDGE
 99     freopen("frame.in","r",stdin);
100     freopen("frame.out","w",stdout);
101 #endif
102     RG int T=gi();
103     while (T--) work();
104     return 0;
105 }

 

posted @ 2017-07-29 12:06  wfj_2048  阅读(482)  评论(0编辑  收藏  举报