ACM Learning process-poj3565 Ants (best match km algorithm)

Source: Internet
Author: User

Young naturalist Bill studies ants in school. His ants feeds on plant-louses this live on apple trees. Each ant colony needs it own apple tree to feed itself.

Bill has a map with coordinates of n ant colonies and n apple trees. He knows that ants travel from their colony to their feeding places and back using chemically tagged routes. The routes cannot intersect each other or ants would get confused and get to the wrong colony or tree, thus spurring a war Between colonies.

Bill would like-to-connect each ant colony-a single apple tree so, all n routes is non-intersecting Strai Ght lines. In this problem such connection are always possible. Your task is to write a program that finds such connection.

On this picture ant colonies is denoted by empty circles and apple trees is denoted by filled circles. One possible connection is denoted by lines.

Input

The first line of the input file contains a single integer number n (1≤ n ≤100)-the number of Ant Co Lonies and apple trees. It is followed by n lines describing n ant colonies, followed by n lines describing n apple trees. Each ant colony and apple tree are described by a pair of integer coordinates x and y (−10 000≤ x y ≤10) on a Cartesian plane. All ant colonies and apple trees occupy distinct points on a plane. No three points is on the same line.

Output

Write to the output file n lines. The number written on i-th line denotes the number (from 1 to N) of the apple tree, that's connected to The i-th ant colony.

Sample Input
5
-42 58
44 86
7 28
99 34
-13-59
-47-44
86 74
68-75
-68 60
99-60
Sample Output
4
2
1
5
3

The problem is to find a line of solid-to-hollow circles in a similar image, one by one, so that 22 segments do not intersect.

One of the ideas is to start with all points to the random connection, and then let the intersection of the line to adjust, here is better understanding, such as AC and BD intersect, then the ad and BC must not intersect. In this way, the number of adjustments required does not seem to be a good calculation.

But, to be sure, the final state is bound to be 22 disjoint.

The AC and BD that intersect above can be found, and must meet AD+BC < AC+BD. This can be proved with a sum greater than the third side of the triangle on both sides of two times. This step makes the point to the inside edge of the weight and become smaller.

So consider, inverse proposition: whether when the edge of AC and BD can be reduced to AD and BC, must be intersect?

It turns out that this proposition is not necessarily, but it can be found that when it can be reduced to AD and BC, AD and BC must be disjoint. Doing so will result in AD+BC > AC+BD.

So as long as can reduce the side of the right and, must be able to ensure that disjoint. Then the final state becomes the edge right and the smallest state, which is the minimum match. Can be done using the KM algorithm.

Appears to be a data problem and cannot be processed using the square of the edge. To use double to save the size of the edge, and then equal to 0 of the judgment, changed to less than EPS.

Code:

#include <iostream>#include<cstdio>#include<cstdlib>#include<cmath>#include<cstring>#include<algorithm>#include<Set>#include<map>#include<queue>#include<string>#defineLL Long Longusing namespacestd;Const intMAXN = the;Const DoubleINF =1e10;intN;structpoint{Doublex, y;} X[MAXN], Y[MAXN];intNX, NY;intLINK[MAXN];DoubleLX[MAXN], LY[MAXN], SLACK[MAXN], W[MAXN][MAXN];//link represents the X-value of the Y-phase, the lx,ly is the top, and the Nx,ny is the number of Y-point sets of the X-point set, respectively .BOOLVISX[MAXN], Visy[maxn];inlineDoubleDisintIintj) {    DoubleAns = (x[i].x-y[j].x) * (x[i].x-y[j].x) + (X[I].Y-Y[J].Y) * (x[i].y-y[j].y); return-sqrt (ans);}BOOLDFS (intx) {Visx[x]=true;  for(inty =1; Y <= ny; y++)    {        if(Visy[y])Continue; Doublet = lx[x]+ly[y]-W[x][y]; if(Fabs (t) < 1e-5) {Visy[y]=true; if(Link[y] = =-1||DFS (Link[y])) {Link[y]=x; return true; }        }        Else if(Slack[y] > t)//does not slack the smallest in the equality sub-graphSlack[y] =T; }    return false;}voidKM () {memset (link,-1,sizeof(link)); memset (ly,0,sizeof(ly));  for(inti =1; I <= NX; i++)//LX initializes the largest of the edges associated with it.         for(intj =1; J <= NY; J + +)            if(J = =1|| W[I][J] >Lx[i]) lx[i]=W[i][j];  for(intx =1; x <= NX; X + +)    {         for(inti =1; I <= NY; i++) Slack[i]=inf;  for (;;) {memset (VISX,false,sizeof(VISX)); memset (Visy,false,sizeof(Visy)); if(DFS (x))//If successful (augmented orbit is found), the point augmentation is completed and the next point is augmented                 Break;//If you fail (no augmented orbit is found), you need to change the label of some points to increase the number of viable edges in the graph. //By subtracting a constant d from all the X-square points in the augmented orbit (that is, traversing through the augmented process)//all the labels of y-square points in the augmented orbit are all added with a constant d            DoubleD =inf;  for(inti =1; I <= NY; i++)                if(!visy[i] && d >Slack[i]) d=Slack[i];  for(inti =1; I <= NX; i++)                if(Visx[i]) lx[i]-=D;  for(inti =1; I <= NY; i++)//after modifying the top label, subtract the slack value of all y vertices that are not in the interlaced tree by D                if(Visy[i]) ly[i]+=D; ElseSlack[i]-=D; }    }     for(inti =1; I <= N; ++i) printf ("%d\n", Link[i]);}voidinput () {NX= NY =N;  for(inti =1; I <= N; ++i) scanf ("%LF%LF", &y[i].x, &y[i].y);  for(inti =1; I <= N; ++i) scanf ("%LF%LF", &x[i].x, &x[i].y);  for(inti =1; I <= N; ++i) for(intj =1; J <= N; ++j) W[i][j]=Dis (i, j);}intMain () {//freopen ("test.in", "R", stdin);     while(SCANF ("%d", &n)! =EOF)        {input ();    KM (); }    return 0;}

ACM Learning process-poj3565 Ants (best match km algorithm)

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.