Ants
Time Limit: 5000MS | Memory Limit: 65536K | |||
Total Submissions: 6904 | Accepted: 2164 | Special Judge |
Description
Young naturalist Bill studies ants in school. His ants feed on plant-louses that live on apple trees. Each ant colony needs its 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 will get confused and get to the wrong colony or tree, thus spurring a war between colonies.
Bill would like to connect each ant colony to a single apple tree so that all n routes are non-intersecting straight lines. In this problem such connection is always possible. Your task is to write a program that finds such connection.
On this picture ant colonies are denoted by empty circles and apple trees are 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 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 is described by a pair of integer coordinates x and y (−10 000 ≤ x, y ≤ 10 000) on a Cartesian plane. All ant colonies and apple trees occupy distinct points on a plane. No three points are on the same line.
Output
Write to the output file n lines with one integer number on each line. The number written on i-th line denotes the number (from 1 to n) of the apple tree that is 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
Source
//# include <bits/stdc++.h>
# include <iostream>
# include <stdio.h>
# include <string.h>
# include <math.h>
using namespace std;
# define eps 1e-
# define LL long long
# define INF 1e20
# define MX
struct Node{
double x,y;
}ant[MX], tree[MX]; int n;
double W[MX][MX];
double Lx[MX], Ly[MX];
int link[MX]; //右边匹配左的
bool S[MX], T[MX]; double dist(int x,int y){
return sqrt((ant[x].x-tree[y].x)*(ant[x].x-tree[y].x)+(ant[x].y-tree[y].y)*(ant[x].y-tree[y].y));
} bool match(int p)
{
S[p]=;
for (int i=;i<=n;i++)
{
if (!T[i]&&(fabs(Lx[p]+Ly[i]-W[p][i])<=eps))
{
T[i]=;
if (link[i]==- || match(link[i]))
{
link[i]=p;
return ;
}
}
}
return ;
} void update()
{
double del = INF;
for (int i=;i<=n;i++) if(S[i])
for (int j=;j<=n;j++) if (!T[j])
if (Lx[i]+Ly[j]-W[i][j] < del - eps)
del = Lx[i]+Ly[j]-W[i][j];
for (int i=;i<=n;i++)
{
if (S[i]) Lx[i]-=del;
if (T[i]) Ly[i]+=del;
}
} void KM()
{
memset(link,-,sizeof(link));
for (int i=;i<=n;i++)
{
Ly[i]=0.0;
Lx[i]=-INF;
for (int j=;j<=n;j++)
if (W[i][j] > Lx[i] + eps)
Lx[i] = W[i][j];
}
for (int i=;i<=n;i++)
{
while ()
{
memset(S,,sizeof(S));
memset(T,,sizeof(T));
if (match(i)) break;
else update();
}
}
} int main()
{
while (scanf("%d",&n)!=EOF)
{
for (int i=;i<=n;i++)
scanf("%lf%lf",&ant[i].x, &ant[i].y);
for (int i=;i<=n;i++)
scanf("%lf%lf",&tree[i].x, &tree[i].y);
for (int i=;i<=n;i++)
for(int j=;j<=n;j++)
W[i][j] = -dist(i,j);
KM();
for(int i = ; i <= n; ++i)
{
for(int j = ; j <= n; ++j)
{
if(link[j] == i)
{
printf("%d\n",j);
break;
}
}
}
}
return ;
}