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 nroutes 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
题意:输入n行蚂蚁坐标,再输入n行苹果树坐标,坐标不会重复,问,怎样匹配才能使蚂蚁和树相连的边不相交,输出1~n行蚂蚁对应的苹果树编号
思路:两点之间,直线最短,只要都是最短距离,必然不相交,求最小完备匹配即可。注意输出是对应苹果树编号,所以我们存图时,把苹果树分到x集合,蚂蚁分到y集合,比较小数是否和0相等,不能用平时的套路,最后就是km算法了,对了由于是求最小完备匹配,所以应该全部存负值。
~~~这道题卡精度把我给卡死了,每天写一道这样的题,不出一个周我就基本崩溃~~~现在被恶心的都不想吃早饭了
#include<stdio.h> #include<string.h> #include<stdlib.h> #include<math.h> #include<algorithm> using namespace std; #define INF 0x3f3f3f3f #define N 110 double PI = 1e-6; int linker[N],visx[N],visy[N],n,nx,ny; double lx[N],ly[N],slack[N]; double map[N][N]; struct node{ double x, y; }; node ant[N],apple[N]; void Getmap() { int i,j; for(i = 1; i <= nx; i ++) for(j = 1; j <= ny; j ++) { double a,b; a = ant[i].x - apple[j].x; b = ant[i].y - apple[j].y; map[j][i] = -sqrt(a*a+b*b);//这样存图可以使x集合为苹果树,y集合为蚂蚁 ,对应输出 } return; } int dfs(int x) { int y; double tmp; visx[x] = 1; for(y = 1; y <= ny; y ++) { if(!visy[y]) { tmp = lx[x] + ly[y] - map[x][y]; if(tmp < PI) { visy[y] = 1; if(linker[y] == -1||dfs(linker[y])) { linker[y] = x; return 1; } } else if(slack[y] > tmp) { slack[y] = tmp; } } } return 0; } int KMP() { int x,i,j; double d;//罪魁祸首!!!! memset(linker,-1,sizeof(linker)); memset(ly,0,sizeof(ly)); for(i = 1; i <= nx; i ++) for(j = 1,lx[i] = -INF; j <= ny; j ++) if(lx[i] < map[i][j]) lx[i] = map[i][j]; for(x = 1; x <= nx; x ++) { for(i = 1; i<= ny; i ++) slack[i] = INF; while(1) { memset(visx,0,sizeof(visx)); memset(visy,0,sizeof(visy)); if(dfs(x)) break; d = INF; for(i = 1; i <= nx; i ++) if(!visy[i]&&d > slack[i]) { d = slack[i]; } for(i = 1; i <= nx; i ++) if(visx[i]) lx[i] -= d; for(i = 1; i <= ny; i ++) if(visy[i]) ly[i] += d; else slack[i]-=d; } } return 1; } int main() { int i; while(scanf("%d",&n)!=EOF) { nx = ny = n; for(i = 1; i <= n; i ++) scanf("%lf%lf",&ant[i].x ,&ant[i].y ); for(i = 1; i <= n; i ++) scanf("%lf%lf",&apple[i].x ,&apple[i].y ); Getmap(); KMP(); for(i = 1; i <= ny; i ++) printf("%d\n",linker[i]);//输出对应的苹果树编号 } return 0; }