Graph theory (km algorithm, Brain puzzle): Hnoi 2014 Frame (frame)

Source: Internet
Author: User

  

A brain puzzle worth writing!

At first, there was no thought, and it was only after reading the solution.

Considering the and representation of A and b as coordinates, then the point that may be the answer must be the point in the lower convex packet, so that the optimization enumeration guarantees efficiency.

However, it is not known exactly which points are the points in the convex package, but we know that the boundary of two points (a minimum of B minimum) must be on the convex hull, if it is known that the points on the two convex hull, we connect them, then the point of the original image is the farthest from this line, it must be the point on the convex hull, so it can be two points, the cross product of the vector to find out

1#include <iostream>2#include <cstring>3#include <cstdio>4 using namespacestd;5 Const intmaxn= -;6 Const intinf=1147483647;7 intA[MAXN][MAXN],B[MAXN][MAXN];8 intW[MAXN][MAXN],SX[MAXN],SY[MAXN];9 intMAT[MAXN],SLA[MAXN];Ten intLX[MAXN],LY[MAXN]; One intn,t; A structdot{ -     intx, y; -Dot (intx_=0,inty_=0): X (x_), Y (y_) {} theFriendBOOL operator==(Dot A,dot b) { -         returna.x==b.x&&a.y==b.y; -     } - }lo,hi; + BOOLDFS (intx) { -sx[x]=1; +      for(inty=1; y<=n;y++) A         if(!Sy[y]) { at             intt=lx[x]+ly[y]-W[x][y]; -             if(t) sla[y]=min (sla[y],t); -             Else{ -sy[y]=1; -                 if(!mat[y]| |DFS (Mat[y])) -{mat[y]=x;return true;} in             }         -         } to     return false;  + } -  the Dot KM () { *memset (LX,0,sizeof(LX)); $memset (Ly,0,sizeof(ly));Panax NotoginsengMemset (Mat,0,sizeof(MAT)); -      for(intI=1; i<=n;i++) the          for(intj=1; j<=n;j++) +lx[i]=Max (lx[i],w[i][j]); A          the      for(intx=1; x<=n;x++){ +memset (SLA, the,sizeof(SLA)); -          while(true){ $memset (SX,0,sizeof(SX)); $memset (SY,0,sizeof(SY)); -             if(DFS (x)) Break; -             intmin=INF; the              for(intI=1; i<=n;i++)if(!sy[i]) min=min (min,sla[i]); -              for(intI=1; i<=n;i++){Wuyi                 if(Sx[i]) lx[i]-=Min; thesy[i]?ly[i]+=min:sla[i]-=Min; -             } Wu         } -     } AboutDOT ret (0,0); $      for(intI=1; i<=n;i++){ -ret.x+=A[mat[i]][i]; -ret.y+=B[mat[i]][i]; -     } A     returnret;  + } the  - intSolve (Dot L,dot r) { $      for(intI=1; i<=n;i++) the          for(intj=1; j<=n;j++) thew[i][j]=a[i][j]* (R.Y-L.Y)-b[i][j]* (r.x-l.x); theDot mid=KM (); the     if(mid==l| | MID==R)returnMin (l.x*l.y,r.x*r.y); -     returnmin (Solve (l,mid), Solve (Mid,r)); in } the  the  About intMain () { theFreopen ("frame.in","R", stdin); theFreopen ("Frame.out","W", stdout); thescanf"%d",&T); +      while(t--){ -scanf"%d",&n); the          for(intI=1; i<=n;i++) for(intj=1; j<=n;j++) scanf ("%d",&A[i][j]); Bayi          for(intI=1; i<=n;i++) for(intj=1; j<=n;j++) scanf ("%d",&B[i][j]);  the          for(intI=1; i<=n;i++) for(intj=1; j<=n;j++) w[i][j]=-a[i][j];lo=KM (); the          for(intI=1; i<=n;i++) for(intj=1; j<=n;j++) w[i][j]=-b[i][j];hi=KM (); -printf"%d\n", Solve (Lo,hi)); -     } the     return 0; the}

  

Graph theory (km algorithm, Brain puzzle): Hnoi 2014 Frame (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.