Ants
Time Limit: 5000MS |
|
Memory Limit: 65536K |
Total Submissions: 5583 |
|
Accepted: 1730 |
|
Special Judge |
Description
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 straight 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 colonies and Apple t Rees. 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 integers coordinates x and y (−10 000≤x, y≤10) on a Cartes Ian 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, which is connected to the i-th ant Colon Y.
Sample Input
5
-42,
7
-13-59
-47-44
68-75
-68
99-60
Sample Output
4
2
1)
5
3
Source Northeastern Europe 2007
Title Link: http://poj.org/problem?id=3565
The main idea: n white dots and n black dots, connect them with n disjoint segments, and ask for a perfectly matched solution that does not intersect.
Title Analysis: According to the sum of the two sides of the triangle is greater than the third side can prove that the minimum weight matching is disjoint, and then the template
#include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std;
int const MAX = 105;
int const INF = 0X3FFFFFFF;
Double const EPS = 1e-6;
int n;
int Visx[max], Visy[max], Lk[max], Ans[max];
Double Lx[max], Ly[max], W[max][max], Slack[max]; struct point {double x, y;}
P[max * 2];
Double Dis (point A, point B) {return sqrt ((a.x-b.x) * (a.x-b.x) + (A.Y-B.Y) * (A.Y-B.Y));}
int DFS (int x) {visx[x] = 1;
for (int y = 1; y <= N; y++) {if (visy[y]) continue;
Double T = lx[x] + ly[y]-w[x][y];
if (T < EPS) {visy[y] = 1; if (lk[y] = =-1 | |
DFS (Lk[y])) {lk[y] = x;
return 1;
}} else if (Slack[y]-T > EPS) slack[y] = t;
} return 0;
} void KM () {memset (LK,-1, sizeof (LK));
memset (ly, 0, sizeof (ly)); memset (LX, 0, sizeof (LX));
for (int i = 1, i <= N; i++) for (int j = 1; J <= N; j + +) if (W[i][j] > Lx[i])
Lx[i] = W[i][j];
for (int x = 1; x <= n; x + +) {for (int i = 1; I <= n; i + +) slack[i] = INF;
while (true) {memset (visx, 0, sizeof (VISX));
memset (Visy, 0, sizeof (Visy));
if (DFS (x)) break;
Double d = INF;
for (int i = 1; I <= n; i++) if (!visy[i] && d > slack[i]) d = slack[i];
for (int i = 1; I <= n; i++) if (Visx[i]) lx[i]-= D;
for (int i = 1; I <= n; i++) {if (Visy[i]) ly[i] + = D;
else Slack[i]-= D;
}}}} int main () {while (scanf ("%d", &n)! = EOF) {memset (w, 0, sizeof (w)); for (int i = 1; I <= 2 * n; i++) scanf ("%lf%lf", &p[i].x, &P[I].Y);
for (int i = 1; l <= N; i++) for (int j = 1; J <= N; j + +) w[i][j]-= DIS (p[i], P[j + n]);
KM ();
for (int i = 1; I <= n; i++) ans[lk[i]] = i;
for (int i = 1; I <= n; i++) printf ("%d\n", Ans[i]); }
}