Ants
Time Limit: 5000ms
Memory Limit: 65536KB
This problem will be judged on PKU. Original ID: 3565
64-bit integer IO format: %lld Java class name: Main
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
解题:看似是计算几何,实则KM算法即可求解
1 #include <iostream> 2 #include <cstdio> 3 #include <climits> 4 #include <cstring> 5 #include <cmath> 6 using namespace std; 7 const int INF = 0x3f3f3f3f; 8 const int maxn = 310; 9 int Link[maxn],n; 10 double W[maxn][maxn],Lx[maxn],Ly[maxn],slack[maxn]; 11 bool S[maxn],T[maxn]; 12 bool match(int u) { 13 S[u] = true; 14 for(int v = 1; v <= n; ++v) { 15 if(T[v]) continue; 16 double d = Lx[u] + Ly[v] - W[u][v]; 17 if(fabs(d) < 1e-6) { 18 T[v] = true; 19 if(Link[v] == -1 || match(Link[v])) { 20 Link[v] = u; 21 return true; 22 } 23 } else if(d < slack[v]) slack[v] = d; 24 } 25 return false; 26 } 27 void update(){ 28 double d = INF; 29 for(int i = 1; i <= n; ++i) 30 if(!T[i] && slack[i] < d) 31 d = slack[i]; 32 for(int i = 1; i <= n; ++i){ 33 if(S[i]) Lx[i] -= d; 34 if(T[i]) Ly[i] += d; 35 else slack[i] -= d; 36 } 37 } 38 double KuhnMunkras() { 39 for(int i = 1; i <= n; ++i) { 40 Lx[i] = Ly[i] = 0; 41 Link[i] = -1; 42 for(int j = 1; j <= n; ++j) 43 Lx[i] = max(Lx[i],W[i][j]); 44 } 45 for(int i = 1; i <= n; ++i){ 46 for(int j = 1; j <= n; ++j) slack[j] = INF; 47 while(true){ 48 memset(S,false,sizeof S); 49 memset(T,false,sizeof T); 50 if(match(i)) break; 51 update(); 52 } 53 } 54 double ret = 0; 55 for(int i = 1; i <= n; ++i) 56 if(Link[i] > -1) ret += W[Link[i]][i]; 57 return ret; 58 } 59 struct Point{ 60 int x,y; 61 }p[maxn]; 62 double calc(int a,int b){ 63 double tmp = (p[a].x - p[b].x)*(p[a].x - p[b].x); 64 tmp += (p[a].y - p[b].y)*(p[a].y - p[b].y); 65 return -sqrt(tmp); 66 } 67 int main() { 68 while(~scanf("%d",&n)){ 69 memset(W,0,sizeof W); 70 for(int i = 0; i < n; ++i) 71 scanf("%d%d",&p[i].x,&p[i].y); 72 for(int i = 0; i < n; ++i) 73 scanf("%d%d",&p[i+n].x,&p[i+n].y); 74 for(int i = 1; i <= n; ++i) 75 for(int j = 1; j <= n; ++j) 76 W[i][j] = calc(i-1,j+n-1); 77 KuhnMunkras(); 78 int ret[maxn]; 79 for(int i = 1; i <= n; ++i) 80 ret[Link[i]] = i; 81 for(int i = 1; i <= n; ++i) 82 printf("%d\n",ret[i]); 83 } 84 return 0; 85 }