POJ3565 Ants "binary graph Best Match"

Source: Internet
Author: User

Topic Links:

http://poj.org/problem?id=3565


Main topic:

In the coordinate system there are n ants, n apple trees, which give you the coordinates of the ant and the apple tree. Let every ant go to an apple tree,

An apple tree corresponds to an ant. So there is n straight line, Q: How to allocate, in order to make the total distance and the smallest, and

N lines do not intersect.


Ideas:

Use a diagram to illustrate the idea.


Suppose A, B are ants, C and D are apple trees. There are two types of matches: the first is AD, BC, and the second is AC, BD.

According to the triangular inequalities AD+BC < AC+BD, the last is a very important property--to meet the sum of the total distance and minimum

The scheme must not intersect. Now to build the two-part map, one side for the ant, the other side for the apple tree, the distance for the edge weights, problems

to find the best match of the minimum right and the right of the two-part graph. To think in reverse, multiply the distance by 1 to create a negative value.

Change to the best match of the maximum weight and the weighted binary graph. Do it directly with the KM algorithm. Km Template Reference Blog:

http://blog.csdn.net/lianai911/article/details/44832831


AC Code:

#include <iostream> #include <algorithm> #include <cstdio> #include <cstring> #include <    cmath>using namespace Std;const int maxn = 110;const double INF = 0xffffffffffff;const double eps = 1e-6;struct node{ Double x, y;} DOT1[MAXN],DOT2[MAXN];d ouble Dist (Node a,node b) {return-sqrt ((a.x-b.x) * (a.x-b.x) + (A.Y-B.Y) * (A.Y-B.Y));} int n,nx,ny;double map[maxn][maxn];int LINK[MAXN];d ouble lx[maxn],ly[maxn],slack[maxn];int Visx[MAXN],visy[MAXN];    int findpath (int u) {visx[u] = 1;        for (int i = 1; I <= NY; ++i) {if (visy[i]) continue;        Double temp = Lx[u] + ly[i]-map[u][i];            if (fabs (temp) <= eps) {visy[i] = 1; if (link[i] = =-1 | |                Findpath (Link[i])) {Link[i] = u;            return 1;        }} else {if (Slack[i] > Temp) slack[i] = temp; }} return 0;}    void KM () {memset (lx,0,sizeof (LX)); memset (ly,0,sIzeof (ly));    memset (link,-1,sizeof (link));                for (int i = 1, i <= NX; ++i) for (int j = 1; j <= NY; ++j) if (Map[i][j] > Lx[i])    Lx[i] = Map[i][j];        for (int i = 1; I <= NX; ++i) {for (int j = 1; j <= NY; ++j) slack[j] = INF;            while (1) {memset (visx,0,sizeof (VISX));            memset (visy,0,sizeof (Visy));            if (Findpath (i)) break;            Double d = INF;            for (int j = 1; j <= NY; ++j) if (!visy[j] && d > slack[j]) d = slack[j];            for (int j = 1; j <= NX; ++j) if (Visx[j]) lx[j]-= D;                for (int j = 1; j <= NY; ++j) {if (Visy[j]) ly[j] + = D;            else Slack[j]-= D;    }}}}int Main () {int N;       while (~SCANF ("%d", &n)) {memset (map,0,sizeof (MAP)); for (int i = 1; I <= N; ++i) scanf ("%lf%lf", &dot1[i].x,&dot1[i].y);        for (int i = 1; I <= N; ++i) scanf ("%lf%lf", &dot2[i].x,&dot2[i].y);         for (int i = 1, i <= N; ++i) for (int j = 1; j <= N; ++j) map[i][j] = Dist (Dot1[i],dot2[j]);        NX = NY = N;        KM (); for (int i = 1, i <= N; ++i) {for (int j = 1; j <= N; ++j) {if (link[j] =                    = i) {printf ("%d\n", j);                Break }}}} return 0;}



POJ3565 Ants "binary graph Best Match"

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.