Ants
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 4125   Accepted: 1258   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

Source

题意:

给你n个黑点和n个白点。叫你找出一种连点的方法使得每一白点连一个黑点。切连线不与其他连线相交。

思路:

看上去跟想几何问题跟KM没什么关系。但确实是KM题。

求连线的最短距离即可了。由于在连线距离最小的条件下。不会有两条直线相交的情况。画个图就知道(三角形两边之和大于第三边)。

须要稍稍的转换下。两点间的价值为距离的负数。还有特别注意答案要求输出每个C相应的A。所以建边时要注意left[i]代表什么!鄙人就为这个WA了好几发。TT。

具体见代码:

#include<algorithm>
#include<iostream>
#include<string.h>
#include<sstream>
#include<stdio.h>
#include<math.h>
#include<vector>
#include<string>
#include<queue>
#include<set>
#include<map>
//#pragma comment(linker,"/STACK:1024000000,1024000000")
using namespace std;
const int INF=0x3f3f3f3f;
const double eps=1e-6;
const double PI=acos(-1.0);
const int maxn=110;
//typedef __int64 ll;
int le[maxn],n;
double lx[maxn],ly[maxn],slack[maxn],w[maxn][maxn],ax[maxn],ay[maxn],bx[maxn],by[maxn];
bool visx[maxn],visy[maxn];
double dist(double x1,double y1,double x2,double y2)
{
double x=x1-x2,y=y1-y2;
return sqrt(x*x+y*y);
}
bool match(int x)
{
int y;
double tp;
visx[x]=true;
for(y=1;y<=n;y++)
{
if(visy[y])
continue;
tp=lx[x]+ly[y]-w[x][y];
if(tp<eps)
{
visy[y]=true;
if(!le[y]||match(le[y]))
{
le[y]=x;
return true;
}
}
else
slack[y]=min(slack[y],tp);
}
return false;
}
void update()
{
int i;
double d; for(i=1,d=INF;i<=n;i++)
if(!visy[i])
d=min(d,slack[i]);
for(i=1;i<=n;i++)
{
if(visx[i])
lx[i]-=d;
if(visy[i])
ly[i]+=d;
else
slack[i]-=d;
}
}
void KM()
{
int i,j,x; memset(le,0,sizeof le);
for(i=1;i<=n;i++)
{
lx[i]=-INF;//注意这里!!
ly[i]=0;
for(j=1;j<=n;j++)
lx[i]=max(lx[i],w[i][j]);
}
for(x=1;x<=n;x++)
{
for(i=1;i<=n;i++)
slack[i]=INF;
while(1)
{
for(i=1;i<=n;i++)
visx[i]=visy[i]=false;
if(match(x))
break;
else
update();
}
}
}
int main()
{
int i,j; while(~scanf("%d",&n))
{
for(i=1;i<=n;i++)
scanf("%lf%lf",&ax[i],&ay[i]);
for(i=1;i<=n;i++)
scanf("%lf%lf",&bx[i],&by[i]);
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
w[j][i]=-dist(ax[i],ay[i],bx[j],by[j]);//这里特别注意!因为答案输出的关系
KM();
for(i=1;i<=n;i++)
printf("%d\n",le[i]);
}
return 0;
}

版权声明:本文博客原创文章。博客,未经同意,不得转载。

poj 2565 Ants (KM+思维)的更多相关文章

  1. poj3565 Ants km算法求最小权完美匹配,浮点权值

    /** 题目:poj3565 Ants km算法求最小权完美匹配,浮点权值. 链接:http://poj.org/problem?id=3565 题意:给定n个白点的二维坐标,n个黑点的二维坐标. 求 ...

  2. POJ 1852 Ants || UVA 10881 - Piotr's Ants 经典的蚂蚁问题

    两题很有趣挺经典的蚂蚁问题. 1.n只蚂蚁以1cm/s的速度在长为L的竿上爬行,当蚂蚁爬到竿子的端点就会掉落.当两只蚂蚁相撞时,只能各自反向爬回去.对于每只蚂蚁,给出距离左端的距离xi,但不知道它的朝 ...

  3. POJ 1852 Ants(贪心)

    POJ 1852 Ants 题目大意 有n只蚂蚁在木棍上爬行,每只蚂蚁的速度都是每秒1单位长度,现在给你所有蚂蚁初始的位置(蚂蚁运动方向未定),蚂蚁相遇会掉头反向运动,让你求出所有蚂蚁都·掉下木棍的最 ...

  4. poj 3565 uva 1411 Ants KM算法求最小权

    由于涉及到实数,一定,一定不能直接等于,一定,一定加一个误差<0.00001,坑死了…… 有两种事物,不难想到用二分图.这里涉及到一个有趣的问题,这个二分图的完美匹配的最小权值和就是答案.为啥呢 ...

  5. POJ 3565 Ants 【最小权值匹配应用】

    传送门:http://poj.org/problem?id=3565 Ants Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: ...

  6. poj 1852 ants 题解《挑战程序设计竞赛》

    地址  http://poj.org/problem?id=1852 题目描述 Description An army of ants walk on a horizontal pole of len ...

  7. poj 3565 ants

    /* poj 3565 递归分治 还有用KM的做法 这里写的分治 按紫书上的方法 不过那里说的有点冗杂了 可以简化一下 首先为啥可以分治 也就是分成子问题解决 只要有一个集合 黑白的个数相等 就一定能 ...

  8. 【POJ3565】ANTS KM算法

    [POJ3565]ANTS 题意:平面上有2*n个点,N白N黑.为每个白点找一个黑点与之连边,最后所有边不交叉.求一种方案. 题解:KM算法真是一个神奇的算法,虽然感觉KM能做的题用费用流都能做~ 本 ...

  9. POJ 3565 Ants(最佳完美匹配)

    Description Young naturalist Bill studies ants in school. His ants feed on plant-louses that live on ...

随机推荐

  1. 冒泡排序算法 C++和PHP达到

    冒泡排序是小元素向前或向后的大要素.两个相邻元件之间的比较结果更.交换也这两个元件之间发生.它是最慢的排序算法. 效率最低的算法. 时间复杂度: 它是最差时间复杂度为:O(n^2),冒泡排序最好的时间 ...

  2. Grub2配置详解(转)

    grub2基础教程-修订版                                      smallapple 目录 一.grub2新特性 二.grub2安装与启动 三.grub2配置文件 ...

  3. cheese desktop内容

    #!/usr/bin/env xdg-open [Desktop Entry] Encoding=UTF- Version=1.0 Type=Application Terminal=false Na ...

  4. J2SE基础:7.系统经常使用类一

    1.Object对象 Object对象是全部对象的根类. 每一个对象都默认继承自Object类. equals():对象与对象之间是否相等. 逻辑上面的相等.equals 物理上面的相等(地址相等) ...

  5. MVC过滤器的详细讲解和示范样本

    MVC共有4个过滤器:ActionFilter(方法过滤器),ResultFilter(结果过滤器.感觉是不是很好,所以称它为),AuthorizationFilter(授权过滤器).Exceptio ...

  6. hdu 4876 ZCC loves cards(暴力)

    题目链接:hdu 4876 ZCC loves cards 题目大意:给出n,k,l,表示有n张牌,每张牌有值.选取当中k张排列成圈,然后在该圈上进行游戏,每次选取m(1≤m≤k)张连续的牌,取牌上值 ...

  7. Peter&#39;s Hobby

    主题链接 题意: 题意比較麻烦.. .n天,给出每天的叶子的一种状态(Dry , Dryish , Damp and Soggy),最有可能出现的天气序列(Sunny, Cloudy and Rain ...

  8. JAVA Socket传输Object(对象)注意的问题

    在java中,可以通过socket将一个对象进行传递,通过ObjectOutputStream,ObjectInputStream来进行写入和读取(具体的方法参考http://blog.csdn.ne ...

  9. JMS分布式应用程序异步消息解决方案EhCache 高速缓存同步问题

    部分博客中描述的使用拦截器怎么用EJB公布的WebService加入缓存,这样能够提高WebService的响应效率.但是即使是这样做,还是要经历网络的传输的.于是决定在调用WebService的程序 ...

  10. hdu4185 Oil Skimming(偶匹配)

    <span style="font-family: Arial; font-size: 14.3999996185303px; line-height: 26px;"> ...