【UVA 1411】 Ants (KM)

时间:2022-05-22 23:28:32

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 the picture ant colonies are denoted by empty circles and apple trees are denoted by filled circles.
One possible connection is denoted by lines.
Input
Input has several dataset. The first line of each dataset 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 (−10000 ≤ x, y ≤ 10000) 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
For each dataset, 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.
Print a blank line between datasets.
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个黑点,要求两两配对,且配对所连线段没有交点。

【分析】

  处理不相交的方法,用点的欧几里德距离作为边权进行KM,那么对于ABCD四个点来说,一定取的是不相交那一种。

因为是有小数点的KM,于是我又打了一下。。。

这种要证明有界性的算法真的打错了一点点就好容易RE,又不会调,(模拟真是太难了,不如肉眼找错= =)

代码如下:

 #include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<cmath>
using namespace std;
#define Maxn 110
#define Maxm 10010
#define INF 0xfffffff struct node
{
int x,y,next;
double c;
}t[Maxm];int len;
int first[Maxn]; double mymax(double x,double y) {return x>y?x:y;}
double mymin(double x,double y) {return x<y?x:y;}
double fabs(double x) {return x<?-x:x;} void ins(int x,int y,double c)
{
t[++len].x=x;t[len].y=y;t[len].c=-c;
t[len].next=first[x];first[x]=len;
} int n;
double lx[Maxn],ly[Maxn],slack[Maxn];
int match[Maxn];
bool visx[Maxn],visy[Maxn]; int ax[Maxn],ay[Maxn],bx[Maxn],by[Maxn]; bool ffind(int x)
{
visx[x]=;
for(int i=first[x];i;i=t[i].next) if(!visy[t[i].y])
{
int y=t[i].y;
if(fabs(lx[x]+ly[y]-t[i].c)<0.00001)
{
visy[y]=;
if(!match[y]||ffind(match[y]))
{
match[y]=x;
return ;
}
}
else slack[y]=mymin(slack[y],lx[x]+ly[y]-t[i].c);
}
return ;
} void solve()
{
memset(match,,sizeof(match));
for(int i=;i<=n;i++)
{
ly[i]=;
lx[i]=-INF;
for(int j=first[i];j;j=t[j].next) lx[i]=mymax(lx[i],t[j].c);
}
int i;
for(i=;i<=n;i++)
{
for(int j=;j<=n;j++) slack[j]=INF;
while()
{
memset(visx,,sizeof(visx));
memset(visy,,sizeof(visy));
if(ffind(i)) break;
double delta=INF;
for(int j=;j<=n;j++) if(!visy[j])
delta=mymin(delta,slack[j]);
if(fabs(delta-INF)<0.00001) return;
for(int j=;j<=n;j++)
{
if(visx[j]) lx[j]-=delta;
if(visy[j]) ly[j]+=delta;
else if(fabs(slack[j]-INF)>0.00001) slack[j]-=delta;
}
}
}
} int main()
{
while(scanf("%d",&n)!=EOF)
{
for(int i=;i<=n;i++) scanf("%d%d",&bx[i],&by[i]);
for(int i=;i<=n;i++) scanf("%d%d",&ax[i],&ay[i]);
// for(int i=1;i<=n;i++) scanf("%d%d",&bx[i],&by[i]);
len=;
memset(first,,sizeof(first));
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
{
double d=(double)((ax[i]-bx[j])*(ax[i]-bx[j])+(ay[i]-by[j])*(ay[i]-by[j]));
d=sqrt(d);
ins(i,j,d);
}
solve();
for(int i=;i<=n;i++) printf("%d\n",match[i]);
}
return ;
}

[UVA 1411]

2016-10-27 14:50:29