Ants - POJ 3565 KM算法

原创 2015年07月06日 21:57:21

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

题意:求一种方式,使得每个蚂蚁与一个苹果树相连,并且线段不相交。

思路:用KM算法,求出最短距离和,即他们不相交。

AC代码如下:

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
int n,nx,ny,linky[110],INF=1e9;
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;
               if(linky[y]==-1 || Find(linky[y]))
               {
                   linky[y]=x;
                   return true;
               }
           }
           else if(slack[y]>t)
             slack[y]=t;
       }
    return false;
}
void KM()
{
    int i,j,k,x,y;
    double d;
    memset(linky,-1,sizeof(linky));
    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++)
           printf("%d\n",linky[i]);
    }
}



poj 3565 Ants KM算法

题目:http://poj.org/problem?id=3565 题意:给你n个蚁群和n个苹果树,用两个数表示其坐标,把每个蚁群对应一棵苹果树,且所有的蚁群和其对应的苹果树的连线不能相交。输出每个蚁...

POJ 3565 Ants (最小权完美匹配 KM算法)

POJ 3565 Ants (最小权匹配 KM算法)

二分图最佳匹配 KM算法 Hdu2255奔小康赚大钱 + Poj 3565 Ants

理论:http://blog.sina.com.cn/s/blog_691ce2b701016reh.html http://philoscience.iteye.com/blog/1754498 ...

POJ_3565 Ants KM匹配

http://poj.org/problem?id=3565 题意: 有N只ants和N棵tree,要求一种ants和tree的匹配方案,使得ant和tree之间的匹配线段互不相交。 思路: ...

poj 3565 Ants KM

题意: 给n只蚂蚁和n课苹果树的坐标,要让每只蚂蚁去一棵苹果树,路线不能重复,求一种可行方案。 分析: 当某种匹配可行时蚂蚁所走的距离和是最小的,可以直接用KM算法求二分图最小权值匹配。还有一种做法...
  • sepNINE
  • sepNINE
  • 2014年12月06日 00:25
  • 1217

poj3565 ants(KM)

题目链接:http://poj.org/problem?id=3565 题意: 有 N 个蚂蚁,N 个苹果,要在每个蚂蚁和一个相应的苹果之间连边,问如何给蚂蚁分配苹果,可以使这些边不相交。分析: ...

poj-3565 Ants(计算几何+调整算法)

题目链接:点击打开链接 Ants Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 5...
  • acm_cxq
  • acm_cxq
  • 2016年05月04日 23:23
  • 214

km算法+slack优化 poj3565

km算法在百度上讲的很清楚讲一些针对这道题的有n只蚂蚁要吃n个苹果,求总路径和最小,且不交叉的方案网上有很多人说是计算几何题其实,研究一下题意可以得出很重要的性质————最优方案一定不交叉。根据三角形...

POJ 3565 KM算法

AntsTime Limit: 5000MS Memory Limit: 65536KTotal Submissions: 1842 Accepted: 527 Special JudgeDescri...

弱校联萌十一大决战之厉兵秣马A ants【八皇后拓展】poj3565

Young naturalist Bill studies ants in school. His ants feed on plant-louses that live on apple trees...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Ants - POJ 3565 KM算法
举报原因:
原因补充:

(最多只允许输入30个字)