# Ants - POJ 3565 KM算法

Ants
 Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 4699 Accepted: 1479 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 ≤ xy ≤ 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

AC代码如下：

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
double lx[110],ly[110],w[110][110],slack[110],x_1[110],y_1[110],x_2[110],y_2[110],eps=1e-9;
bool visx[110],visy[110];
int dcmp(double x){return (x>eps)-(x<-eps);}
bool Find(int x)
{
int y;
double t;
visx[x]=1;
for(y=1;y<=ny;y++)
if(!visy[y])
{
t=lx[x]+ly[y]-w[x][y];
if(dcmp(t)==0)
{
visy[y]=1;
{
return true;
}
}
else if(slack[y]>t)
slack[y]=t;
}
return false;
}
void KM()
{
int i,j,k,x,y;
double d;
memset(ly,0,sizeof(ly));
for(i=1;i<=nx;i++)
{
lx[i]=-INF;
for(j=1;j<=ny;j++)
lx[i]=max(lx[i],w[i][j]);
}
for(i=1;i<=nx;i++)
{
for(j=1;j<=ny;j++)
slack[j]=INF;
while(true)
{
memset(visx,0,sizeof(visx));
memset(visy,0,sizeof(visy));
if(Find(i))
break;
d=INF;
for(j=1;j<=ny;j++)
if(!visy[j] && d>slack[j])
d=slack[j];
for(j=1;j<=nx;j++)
if(visx[j])
lx[j]-=d;
for(j=1;j<=ny;j++)
if(visy[j])
ly[j]+=d;
else
slack[j]-=d;
}
}
}
int main()
{
int i,j,k;
while(~scanf("%d",&n))
{
nx=ny=n;
for(i=1;i<=n;i++)
scanf("%lf%lf",&x_2[i],&y_2[i]);
for(i=1;i<=n;i++)
scanf("%lf%lf",&x_1[i],&y_1[i]);
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
w[i][j]=-sqrt((x_1[i]-x_2[j])*(x_1[i]-x_2[j])+(y_1[i]-y_2[j])*(y_1[i]-y_2[j]));
KM();
for(i=1;i<=n;i++)
}
}


• 本文已收录于以下专栏：

## POJ3565 Ants【二分图最佳匹配】

• u011676797
• 2015年04月02日 19:37
• 730

## poj 3565 Ants KM算法

• discreeter
• 2015年12月13日 15:52
• 367

## poj 3686(最小费用最大流来搞KM算法：建图还是挺难想到的)

• Weiguang_123
• 2012年08月18日 22:21
• 3702

## poj3565Ants【线段相交判断】

Language: Default Ants Time Limit: 5000MS   Memory Limit: 65536K Total Submissio...
• R1986799047
• 2015年09月21日 20:54
• 290

## Ants POJ - 3565 （神题）

• qq_30927651
• 2017年01月08日 21:42
• 114

## poj 3565 Ants 最小权匹配

Language: Default Ants Time Limit: 5000MS   Memory Limit: 65536K Total Submissio...
• u011645923
• 2014年07月16日 12:21
• 791

## poj 3565 Ants KM

• sepNINE
• 2014年12月06日 00:25
• 1248

## poj 3565 Ants （KM算法）

• clover_hxy
• 2017年04月12日 07:03
• 151

## POJ 3565 Ants （KM）

• lwt36
• 2015年10月05日 22:26
• 215

## POJ 3565 Ants (证明+KM算法)

• verticallimit
• 2014年04月22日 08:48
• 869

举报原因： 您举报文章：Ants - POJ 3565 KM算法 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)