"Algorithm" minimum product spanning Tree & minimum product match (HNOI2014 frame)

Source: Internet
Author: User

Today, the exam is too difficult, so I gave up ... Instead, we learned the smallest product spanning tree.

Minimum product spanning Tree definition:

(Excerpt from an online blog post).

The main problem we solve is that when k = 2 o'clock, how to get the minimum product of weight value. We note that a graph can have a number of spanning trees, and we write the weights of each spanning tree as (x, y), representing the sum of the first weights as x, and the sum of the second weights as Y. Thus, it is natural to associate the coordinates on the two-dimensional plane, and each spanning tree is a point on the plane. The point we want to find is the smallest point of X * y. Where is this point? Obviously, if X1 <= x2, the weight of Y1 <= y2,1 point is necessarily smaller. So our answer is only likely to be on the lower convex shell of the convex hull on this floor plan.

So we found a, b two point, a nearest to the Y axis, a distance from the x-axis nearest, these two points must be the lower convex shell of the two endpoints. After that, we find the point C, which is farthest from the ab distance, and then solve the problem by using the AC,BC to update the answer and then recursively to the new edge. Here's the question: How do I find this one that is farthest away and at the C point below AB? We convert distances into areas and use cross-product solutions. Because the C point is required below AB, the resulting cross product must be negative. And because | Fork Product | = Quadrilateral area, so the resulting cross product must be the largest absolute value in negative values, that is, to solve a cross product with AB is the smallest C point.

Then we begin to consider the transformation of the formula: Min (b-a) * (c-a) = (b.x-a.x) (c.x-a.x)-(c.x-a.x) (B.Y-A.Y); To open this equation and omit the constant part, we find that the request is (A.Y-B.Y) * A[i][j]-(a.x-b.x) * B[i][j] the smallest. We consider this thing as a weight, and we can use Kruskal to find the lowest C point of this value. If it is a match, then I--j as a matching weight, the weight of the inverse (because of the minimum required) run KM algorithm to obtain the maximum weight matching.

The following code is modeled, but feel that the writing is very beautiful, put here you can refer to. Thank the original blogger ~

#include <bits/stdc++.h>using namespacestd;#defineMAXN 1000#defineINF 99999999intN, ans =INF, LX[MAXN], LY[MAXN], S[MAXN], MATCH[MAXN];intT, G[MAXN][MAXN], A[MAXN][MAXN], B[MAXN][MAXN];BOOLVISX[MAXN], VISY[MAXN];structvec{intx, y;}; VECoperator-(Vec A, Vec b) {return(VEC) {b.x-a.x, B.y-a.y};}int operator*(Vec A, Vec b) {returna.x * B.Y-A.Y *b.x;}intRead () {intx =0, k =1; CharC; C=GetChar ();  while(C <'0'|| C >'9') {if(c = ='-') K =-1; c =GetChar ();}  while(c >='0'&& C <='9') x = x *Ten+ C-'0', C =GetChar (); returnX *K;}structgraph{voidBuildintWxintWY) {         for(inti =1; I <= N; i + +)             for(intj =1; J <= N; J + +) G[i][j]=-(WX * A[I][J] + WY *B[i][j]); }            BOOLDfsintu) {Visx[u]=1;  for(intv =1; V <= N; V + +)        {            if(Visy[v])Continue; intTEM = Lx[u] + ly[v]-G[u][v]; if(!tem) {Visy[v]=1; if(!match[v] | |DFS (Match[v])) {Match[v]=u; return 1; }            }            ElseS[V] =min (s[v], tem); }        return false; } VEC () {memset (LX,0,sizeof(LX)), memset (ly,0,sizeof(ly)); memset (Match,0,sizeof(match));  for(inti =1; I <= N; i + +)             for(intj =1; J <= N; J + +) Lx[i]=Max (Lx[i], g[i][j]);  for(inti =1; I <= N; i + +) {memset (s), the,sizeof(s));  while(23333) {memset (VISX,0,sizeof(VISX)), memset (Visy,0,sizeof(Visy)); if(Dfs (i)) Break; intTEM =INF;  for(intj =1; J <= N; J + +)                    if(!visy[j]) tem =min (tem, s[j]);  for(intj =1; J <= N; J + +)                    if(Visx[j]) lx[j]-=tem;  for(intj =1; J <= N; J + +)                    if(Visy[j]) ly[j] + =tem; ElseS[J]-=tem; }} VEC re; Re.x=0, re.y =0;  for(inti =1; I <= N; i + +) re.x+ = A[match[i]][i], re.y + =B[match[i]][i]; returnre; }}g;voidSolve (Vec A, Vec B) {g.build (a.y-B.y, b.x-a.x); VEC C=g.km (); Ans= min (ans, c.x *c.y); if(A-B) * (A-C) >=0)return; Solve (A, C), Solve (c, B);intMain () {T=read ();  while(T--) {n=read ();  for(inti =1; I <= N; i + +)             for(intj =1; J <= N; J + +) A[i][j]=read ();  for(inti =1; I <= N; i + +)             for(intj =1; J <= N; J + +) B[i][j]=read (); G.build (1,0); VEC A=g.km (); G.build (0,1); VEC B=g.km (); Ans= Min (a.x * a.y, b.x *b.y);        Solve (A, B); printf ("%d\n", ans); }    return 0;} 

"Algorithm" minimum product spanning Tree & minimum product match (HNOI2014 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.