"Bzoj 3571" [Hnoi2014] picture frame

Source: Internet
Author: User

3571: [Hnoi2014] Picture frame

Time limit:20 Sec Memory limit:128 MB
submit:513 solved:292
[Submit] [Status] [Discuss]
Description

Little T prepared to put a few pictures at home, for which he bought N pictures and N frames. In order to reflect his taste, little t hope to be able to properly match the picture and picture frame, making it appear neither too mediocre nor too vainly disobey. For the pairing of the first picture with the first frame, little t gives the common degree of AIJ and vainly disobey degree bij of this pairing. The overall disharmony of the whole collocation scheme is the sum of the ordinary degree of each pair of picture and picture frame and the vainly disobey degree of each pair of picture and picture frame. In particular, in the collocation scheme of the first picture and the first pi frame pair, the overall disharmony degree is

Little T wants to know what the smallest total disharmony can be achieved by collocation.

Input

The input file line is a positive integer t, representing the number of data groups, followed by the T-group data.
For each set of data, the first line is a positive integer n, which indicates that there are n pairs of pictures and frames.
2nd to n+1, each line has n non-negative integers, and the number of rows J of I+1 indicates AIJ.
N+2 to 2*n+1, each line has N non-negative integers, and the number of rows J i+n+1 is bij.

Output

Contains t-lines, one integer per line, representing the smallest overall disharmony

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

1th picture with 3rd picture frame, 2nd picture with 1th frame, 3rd picture with 2nd picture frame, the overall disharmony degree is 30

n<=70,t<=3,aij<=200,bij<=200

KM algorithm + minimum product spanning tree.

When you see the formula, you can think of the minimum product spanning tree (see "Bzoj 2395")

Then we use the KM algorithm to find a match, with the smallest product generation tree to do it.

Attention:
Use the minimum product generation tree to pay attention to a detail, because the KM algorithm is to find the maximum match, so when the calculation we have to take the edge of the right to the opposite number, that is, in the process of division of the right side to take the opposite number!

#include <algorithm>#include <cmath>#include <cstdio>#include <cstring>#include <cstdlib>#define M#define INF 1000000000000000LL#define LL Long Longusing namespace STD;intVX[M],N,VY[M],MATCH[M]; LL W[m][m],a[m][m],b[m][m],ans=inf; LL Slack[m],lx[m],ly[m];structpoint{LL x, y;} b;BOOLDfsintx) {vx[x]=1; for(intI=1; i<=n;i++)if(!vy[i]) {LL t=lx[x]+ly[i]-w[x][i];if(!t) {vy[i]=1;if(!match[i]| | DFS (Match[i])) {match[i]=x;return 1; }             }ElseSlack[i]=min (slack[i],t); }return 0;} Point KM () { for(intI=1; i<=n;i++) match[i]=0, ly[i]=0; for(intI=1; i<=n;i++) {lx[i]=-inf; for(intj=1; j<=n;j++) Lx[i]=max (Lx[i],w[i][j]); } for(intI=1; i<=n;i++) { for(intj=1; j<=n;j++) Slack[j]=inf; while(1)        {memset(VX,0,sizeof(VX));memset(VY,0,sizeof(VY));if(Dfs (i)) Break; LL D=inf; for(intj=1; j<=n;j++)if(!vy[j]&&slack[j]<d) d=slack[j]; for(intj=1; j<=n;j++)if(Vx[j]) Lx[j]-=d; for(intj=1; j<=n;j++)if(Vy[j]) Ly[j]+=d;ElseSlack[j]-=d;    }}, point P; p.x=0, p.y=0; for(intI=1; i<=n;i++) P.x+=a[match[i]][i],p.y+=b[match[i]][i]; Ans=min (ANS,P.X*P.Y);returnP;} LL Cross (Point a,point b,point c) {return(b.x-a.x) * (C.Y-A.Y)-(c.x-a.x) * (B.Y-A.Y);}voidSolve (Point A,point B) {LL y=b.y-a.y,x=a.x-b.x; for(intI=1; i<=n;i++) for(intj=1; j<=n;j++) W[i][j]=y*a[i][j]+x*b[i][j]; Point p=km ();if(Cross (P,A,B) >=0)return;    Solve (A,P); Solve (p,b);}intMain () {intTscanf("%d", &t); while(t--) {Ans=inf;scanf("%d", &n); for(intI=1; i<=n;i++) for(intj=1; j<=n;j++) {scanf("%lld", &a[i][j]);            W[I][J]=-A[I][J]; } for(intI=1; i<=n;i++) for(intj=1; j<=n;j++)scanf("%lld", &b[i][j]); A=KM (); for(intI=1; i<=n;i++) for(intj=1; j<=n;j++) W[i][j]=-b[i][j];        B=KM (); Solve (A, b);printf("%lld\n", ans); }return 0;}

"Bzoj 3571" [Hnoi2014] picture frame

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.