Antstime limit:5000msmemory limit:65536kbthis problem'll be judged onPKU. Original id:3565
64-bit integer IO format: %lld Java class name: Main
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 Strai Ght 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 Co Lonies 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 are described by a pair of integer coordinates x and y (−10 000≤ x y ≤10) on a Cartesian 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 t o The i-th ant colony.
Sample Input
5-42 5844 867 2899 34-13-59-47-4486 7468-75-68 6099-60
Sample Output
42153
SourceNortheastern Europe 2007 Problem Solving: Seemingly computational geometry, the KM algorithm can be solved
1#include <iostream>2#include <cstdio>3#include <climits>4#include <cstring>5#include <cmath>6 using namespacestd;7 Const intINF =0x3f3f3f3f;8 Const intMAXN =310;9 intLink[maxn],n;Ten DoubleW[MAXN][MAXN],LX[MAXN],LY[MAXN],SLACK[MAXN]; One BOOLS[MAXN],T[MAXN]; A BOOLMatchintu) { -S[u] =true; - for(intv =1; V <= N; ++v) { the if(T[v])Continue; - DoubleD = lx[u] + ly[v]-W[u][v]; - if(Fabs (d) < 1e-6) { -T[V] =true; + if(Link[v] = =-1||match (Link[v])) { -LINK[V] =u; + return true; A } at}Else if(d < slack[v]) slack[v] =D; - } - return false; - } - voidUpdate () { - DoubleD =INF; in for(inti =1; I <= N; ++i) - if(! T[i] && Slack[i] <d) toD =Slack[i]; + for(inti =1; I <= N; ++i) { - if(S[i]) Lx[i]-=D; the if(T[i]) Ly[i] + =D; * ElseSlack[i]-=D; $ }Panax Notoginseng } - DoubleKuhnmunkras () { the for(inti =1; I <= N; ++i) { +Lx[i] = Ly[i] =0; ALink[i] =-1; the for(intj =1; J <= N; ++j) +Lx[i] =Max (lx[i],w[i][j]); - } $ for(inti =1; I <= N; ++i) { $ for(intj =1; J <= N; ++J) Slack[j] =INF; - while(true){ -memset (S),false,sizeofS); thememset (T,false,sizeofT); - if(Match (i)) Break;Wuyi update (); the } - } Wu DoubleRET =0; - for(inti =1; I <= N; ++i) About if(Link[i] >-1) ret + =W[link[i]][i]; $ returnret; - } - structpoint{ - intx, y; A }P[MAXN]; + DoubleCalcintAintb) { the DoubleTMP = (p[a].x-p[b].x) * (p[a].x-p[b].x); -TMP + = (p[a].y-p[b].y) * (P[A].Y-p[b].y); $ return-sqrt (TMP); the } the intMain () { the while(~SCANF ("%d",&N)) { thememset (W,0,sizeofW); - for(inti =0; I < n; ++i) inscanf"%d%d",&p[i].x,&p[i].y); the for(inti =0; I < n; ++i) thescanf"%d%d", &p[i+n].x,&p[i+n].y); About for(inti =1; I <= N; ++i) the for(intj =1; J <= N; ++j) theW[I][J] = Calc (i-1, j+n-1); the Kuhnmunkras (); + intRET[MAXN]; - for(inti =1; I <= N; ++i) theRet[link[i]] =i;Bayi for(inti =1; I <= N; ++i) theprintf"%d\n", Ret[i]); the } - return 0; -}
View Code
POJ 3565 Ants