uva 1411 Ants (权值和最小的完美匹配---KM算法)
uva 1411 Ants
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
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 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条不相交的线段把它们连接起来。当中每条线段恰好连接一个白点和一个黑点,每一个点恰好连接到一条线段。
解题思路:点的坐标已经给出,那么点与点之间的距离就是它们的欧几里得距离(((x1−x2)2+(y1−y2)2)−−−−−−−−−−−−−−−−−−−−√事实上就是这个)。这题的难点在于,怎样解决线段之间不能相交的问题。增加线段(a,b)和(c,d)相交,那么一定有线段(a,c)和(b,d)的权值和小于它。所以为了满足权值和最小的完美匹配,匹配时是不会出现线段交叉的问题的。
怎样用权值和最大的完美匹配模板求权值和最小的完美匹配?有两种方法:
1)算距离时,把权值读为负数。
2)读入权值时,用一个非常大的数,减去全部的权值。记录的是减出来的差。
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdlib>
using namespace std;
const int N = 205;
const int INF = 50005;
typedef long long ll;
int n;
double X1[N], Y1[N];
double X2[N], Y2[N];
double W[N][N];
double Lx[N], Ly[N];
int left[N];
bool S[N], T[N];
bool match(int i) {
S[i] = true;
for (int j = 1; j <= n; j++) {
if (fabs(Lx[i] + Ly[j] - W[i][j]) < 1e-9 && !T[j]) {
T[j] = true;
if (!left[j] || match(left[j])) {
left[j] = i;
return true;
}
}
}
return false;
}
void update() {
double a = INF;
for (int i = 1; i <= n; i++) {
if (!S[i]) continue;
for (int j = 1; j <= n; j++) {
if (T[j]) continue;
a = min(a, Lx[i] + Ly[j] - W[i][j]);
}
}
for (int i = 1; i <= n; i++) {
if (S[i]) Lx[i] -= a;
if (T[i]) Ly[i] += a;
}
}
void KM() {
for (int i = 1; i <= n; i++) {
left[i] = Lx[i] = Ly[i] = 0;
for (int j = 1; j <= n; j++) {
Lx[i] = max(Lx[i], W[i][j]);
}
}
for (int i = 1; i <= n; i++) {
while (1) {
for (int j = 1; j <= n; j++) S[j] = T[j] = 0;
if (match(i)) break;
else update();
}
}
}
double getDis(int x, int y) {
return sqrt(pow(X1[x] - X2[y], 2) + pow(Y1[x] - Y2[y], 2));
}
void input() {
for (int i = 1; i <= n; i++) {
scanf("%lf %lf", &X1[i], &Y1[i]);
}
for (int i = 1; i <= n; i++) {
scanf("%lf %lf", &X2[i], &Y2[i]);
}
}
void getW() {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
W[j][i] = -getDis(i, j);
}
}
}
int main() {
while (scanf("%d", &n) != EOF) {
input();
getW();
KM();
for (int i = 1; i <= n; i++) {
printf("%d\n", left[i]);
}
}
return 0;
}
uva 1411 Ants (权值和最小的完美匹配---KM算法)的更多相关文章
- UVA 1411 - Ants(二分图完美匹配)
UVA 1411 - Ants 题目链接 题意:给定一些黑点白点,要求一个黑点连接一个白点,而且全部线段都不相交 思路:二分图完美匹配,权值存负的欧几里得距离,这种话,相交肯定比不相交权值小,所以做一 ...
- poj 3565 uva 1411 Ants KM算法求最小权
由于涉及到实数,一定,一定不能直接等于,一定,一定加一个误差<0.00001,坑死了…… 有两种事物,不难想到用二分图.这里涉及到一个有趣的问题,这个二分图的完美匹配的最小权值和就是答案.为啥呢 ...
- 【uva 1411 Ants蚂蚁们】
题目大意: ·给你一个n,表示输入n个白点和n个黑点(输入每一个点的坐标).现在需要将各个白点和各个黑点一一用线段连接起来,需要满足这些线段不能够相交. ·特色: 我们如何保证线段间不相交. ·分析: ...
- hdu 1853 Cyclic Tour (二分匹配KM最小权值 或 最小费用最大流)
Cyclic Tour Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/65535 K (Java/Others)Total ...
- uva 1411 Ants
题意: 一个平面上有n个黑色的点,n个白色的点,要求黑色的点与白色点之间一一配对,且线段之间不相交. 思路: 线段不相交并不好处理,想了很久想不出,所以看了蓝书的讲解. 一个很明显的结论是,不相交的线 ...
- 非负权值有向图上的单源最短路径算法之Dijkstra算法
问题的提法是:给定一个没有负权值的有向图和其中一个点src作为源点(source),求从点src到其余个点的最短路径及路径长度.求解该问题的算法一般为Dijkstra算法. 假设图顶点个数为n,则针对 ...
- ACM学习历程—POJ3565 Ants(最佳匹配KM算法)
Young naturalist Bill studies ants in school. His ants feed on plant-louses that live on apple trees ...
- HDU2255-奔小康赚大钱-二分图最大权值匹配-KM算法
二分图最大权值匹配问题.用KM算法. 最小权值的时候把权值设置成相反数 /*-------------------------------------------------------------- ...
- 二分图带权匹配 KM算法与费用流模型建立
[二分图带权匹配与最佳匹配] 什么是二分图的带权匹配?二分图的带权匹配就是求出一个匹配集合,使得集合中边的权值之和最大或最小.而二分图的最佳匹配则一定为完备匹配,在此基础上,才要求匹配的边权值之和最大 ...
随机推荐
- HDU_1542_(树状数组)
Stars Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submi ...
- 前复权是从今天的价格倒推 后复权是从上市价格前推 不复权就是原始K线。
前复权是从今天的价格倒推 后复权是从上市价格前推 不复权就是原始K线.
- CSS 文字换行与不换行
1. 强制不换行 p{ white-space:nowrap; } 2. 自动换行 p{ word-wrap: break-word; word-break: normal; } 3. 强制英文单词断 ...
- ssh设置免秘钥登录
系统:CentOS7.2 需求:A服务器的fab用户需要使用www用户免秘钥登录到B服务器 换句话说,就是在A服务器的fab用户下,ssh www@B服务ip -p 端口 ,然后登录过去 ...
- bash之数组
Bash 提供索引和联想的一维数组变量.可用作任何变量 索引的数组 :内置的声明将显式声明数组.有没有最大限制 一个数组,也没有要求成员将索引或连续分配的大小.索引的数组 引用使用 (包括算术表达式) ...
- laravel JWTAuth实现api接口鉴权(基础篇)
官网:https://jwt-auth.readthedocs.io 参考:https://learnku.com/articles/10885/full-use-of-jwt#99529f 1.to ...
- 判断Exe(DLL)和符号文件是否匹配---验证模块和符号文件是否匹配的工具和方法
当我们进行程序调试时,有时调试器会直接告诉你符号文件不对,或则显示出的调用栈不对,当你怀疑符号文件不匹配时,如何确定呢? 如果是用windbg调试,请用 !chksym 模块名比如,匹配的时候 不匹 ...
- CPU怎么计算1+1----CPU计算的电路基础
从<十进制和二进制的运算---我所理解到的人类的运算的本质>这里我们知道,人类进行运算的本质是查表,并且我们存储的表是有限的.那么计算机是怎进行四则运算的呢,也是查表吗,肯定不是,今天,我 ...
- 10CSS高级滤镜
CSS高级滤镜 滤镜特效的应用 filter:滤镜属性(参数1,参数2,……) filter是滤镜属性选择符. 透明度——alpha opacity代表透明度等级,参数值从0到100,从完全透明 ...
- ansible基础配置
1.基础配置 1.1.环境 主机配置 ansible版本:2.7.4 控制端:centos7.4,IP:192.168.1.213,主机名:operation 被控制端: centos6.5,IP:1 ...