Ants

Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 6904   Accepted: 2164   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 个树,每个蚂蚁要连一个树,并且路径不能相交,求任意一种方案
输入: n,n 个蚂蚁的坐标,n 个树的坐标,
输出: n 个蚂蚁相连的树的编号
 
//对于输入输出还是要小心点,这题就是二分图最佳完美匹配的模板题了,因为不能相交,容易想明白。
建一个以距离的负值为权的图跑算法即可。
 //# include <bits/stdc++.h>
# include <iostream>
# include <stdio.h>
# include <string.h>
# include <math.h>
using namespace std;
# define eps 1e-
# define LL long long
# define INF 1e20
# define MX
struct Node{
double x,y;
}ant[MX], tree[MX]; int n;
double W[MX][MX];
double Lx[MX], Ly[MX];
int link[MX]; //右边匹配左的
bool S[MX], T[MX]; double dist(int x,int y){
return sqrt((ant[x].x-tree[y].x)*(ant[x].x-tree[y].x)+(ant[x].y-tree[y].y)*(ant[x].y-tree[y].y));
} bool match(int p)
{
S[p]=;
for (int i=;i<=n;i++)
{
if (!T[i]&&(fabs(Lx[p]+Ly[i]-W[p][i])<=eps))
{
T[i]=;
if (link[i]==- || match(link[i]))
{
link[i]=p;
return ;
}
}
}
return ;
} void update()
{
double del = INF;
for (int i=;i<=n;i++) if(S[i])
for (int j=;j<=n;j++) if (!T[j])
if (Lx[i]+Ly[j]-W[i][j] < del - eps)
del = Lx[i]+Ly[j]-W[i][j];
for (int i=;i<=n;i++)
{
if (S[i]) Lx[i]-=del;
if (T[i]) Ly[i]+=del;
}
} void KM()
{
memset(link,-,sizeof(link));
for (int i=;i<=n;i++)
{
Ly[i]=0.0;
Lx[i]=-INF;
for (int j=;j<=n;j++)
if (W[i][j] > Lx[i] + eps)
Lx[i] = W[i][j];
}
for (int i=;i<=n;i++)
{
while ()
{
memset(S,,sizeof(S));
memset(T,,sizeof(T));
if (match(i)) break;
else update();
}
}
} int main()
{
while (scanf("%d",&n)!=EOF)
{
for (int i=;i<=n;i++)
scanf("%lf%lf",&ant[i].x, &ant[i].y);
for (int i=;i<=n;i++)
scanf("%lf%lf",&tree[i].x, &tree[i].y);
for (int i=;i<=n;i++)
for(int j=;j<=n;j++)
W[i][j] = -dist(i,j);
KM();
for(int i = ; i <= n; ++i)
{
for(int j = ; j <= n; ++j)
{
if(link[j] == i)
{
printf("%d\n",j);
break;
}
}
}
}
return ;
}
 

Ants(二分图最佳完美匹配)的更多相关文章

  1. UVa1349 Optimal Bus Route Design(二分图最佳完美匹配)

    UVA - 1349 Optimal Bus Route Design Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & ...

  2. UVa 11383 少林决胜(二分图最佳完美匹配)

    https://vjudge.net/problem/UVA-11383 题意: 给定一个N×N矩阵,每个格子里都有一个正整数W(i,j).你的任务是给每行确定一个整数row(i),每列也确定一个整数 ...

  3. UVA - 1045 The Great Wall Game(二分图最佳完美匹配)

    题目大意:给出棋盘上的N个点的位置.如今问将这些点排成一行或者一列.或者对角线的最小移动步数(每一个点都仅仅能上下左右移动.一次移动一个) 解题思路:暴力+二分图最佳完美匹配 #include < ...

  4. 【LA4043 训练指南】蚂蚁 【二分图最佳完美匹配,费用流】

    题意 给出n个白点和n个黑点的坐标,要求用n条不相交的线段把他们连接起来,其中每条线段恰好连接一个白点和一个黑点,每个点恰好连接一条线段. 分析 结点分黑白,很容易想到二分图.其中每个白点对应一个X结 ...

  5. Uva1349Optimal Bus Route Design(二分图最佳完美匹配)(最小值)

    题意: 给定n个点的有向图问,问能不能找到若干个环,让所有点都在环中,且让权值最小,KM算法求最佳完美匹配,只不过是最小值,所以把边权变成负值,输出时将ans取负即可 这道题是在VJ上交的 #incl ...

  6. ZOJ-3933 Team Formation (二分图最佳完美匹配)

    题目大意:n个人,分为两个阵营.现在要组成由若干支队伍,每支队伍由两个人组成并且这两个人必须来自不同的阵营.同时,每个人都有m个厌恶的对象,并且厌恶是相互的.相互厌恶的人不能组成一支队伍.问最多能组成 ...

  7. UVa 1349 - Optimal Bus Route Design(二分图最佳完美匹配)

    链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

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

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

  9. UVALive 4043 Ants 蚂蚁(二分图最佳完美匹配,KM算法)

    题意: 有n个蚂蚁n棵树,蚂蚁与树要配对,在配对成功的一对之间连一条线段,要求所有线段不能相交.按顺序输出蚂蚁所匹配的树. 思路: 这个题目真是技巧啊,不能用贪心来为每个蚂蚁选择最近的树,这样很可能是 ...

随机推荐

  1. Linux 后台开发常用命令

    1.automake编译 autoreconf -isv ./configure make 2.远程获取代码 rsync -aPvr --exclude=*/log/* *.*.*.*::home/s ...

  2. scp命令使下用

    先说下背景把,使用ubuntu,没有windows以下到xshell软件管理一下vps.正瞅着须要下载一个chrome(chrominum真的不好用).此刻大谷歌正墙外呢,无奈挂着ss firefox ...

  3. flask 框架 前端和后端请求超时问题

    部署模式 flask + Gunicorn + nginx 为什么要用Gunicorn + nginx ? 请看知乎大神们的回答:https://www.zhihu.com/question/3852 ...

  4. Python 的错误和异常处理

    语法错误 Python 的语法错误或者称之为解析错,如下: >>> while True print('Hello world') File "<stdin>& ...

  5. Python进行数值计算

    1.计算积分 (1)计算定积分 from scipy import integrate #定义函数def half_circle(x): return (1-x**2)**0.5 pi_half, e ...

  6. centos7单机版安装hbase

    1.首先安装jdk1.8 yum install java-1.8.0-openjdk* -y 2.下载hbase 地址:http://mirrors.shuosc.org/apache/hbase/ ...

  7. ViewStub 的使用

    一.内容概述 举例说明ViewStub标签的使用 二.ViewStub类的文档说明及应用场举例 文档描述: A ViewStub is an invisible, zero-sized View th ...

  8. cordova开发自己定义插件

    以下是自己定义cordova插件的基本入门.做插件的小白可以參考一下哈,兴许会更新插件的进阶博客,希望大家可以共同学习共同进步 1.环境搭建 cordova插件开发前须要安装一些软件和配置环境 1.1 ...

  9. HTML学习笔记(四)

    1.       css行内样式要注意的问题: a)     stylekeyword要作为标签的一个属性写在标签内: b)     等号后面是一对双引號包括的内容. c)      Style属性和 ...

  10. Atitit.自定义jdbc驱动  支持jsql

    Atitit.自定义jdbc驱动  支持jsql 1. 为什么需要自定义驱动1 1.1. 透明分库分表1 1.2. 自定义数据库的接口.比如大数据文档文件类型的数据库,数据存储引擎2 2. 整个文章分 ...