传送门:http://poj.org/problem?id=3565

Ants
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 7650   Accepted: 2424   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 个蚂蚁的坐标 ( x1, y1 ), N 个苹果树的坐标 ( x2, y2 ),将蚂蚁和苹果树两两配对,但是路径不能相交。

按顺序输出 第 i 个蚂蚁匹配到第几棵苹果树。

解题思路:

最小权值匹配的应用:做小权值匹配就保证了两两不相交,为什么呢?

这是因为如果最小权匹配有两条线段相交,那么一定可以转化为两条不相交且总长度更短的两条线段(三角形斜边一定小于另外两边之和嘛),这与最小权矛盾,所以可以证明最小权匹配中没有相交的线段。

AC code:

 #include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
const int MAXN = ;
const double INF = 0x3f3f3f3f;
struct date
{
double x, y;
}node1[MAXN], node2[MAXN]; double a[MAXN][MAXN]; //二分图;
double ex[MAXN], ey[MAXN];
int linker[MAXN];
bool visx[MAXN], visy[MAXN];
double slack[MAXN];
int N; bool Find(int x)
{
visx[x] = true;
for(int y = ; y <= N; y++){
if(visy[y]) continue;
double t = ex[x] + ey[y] - a[x][y];
if(t < 1e-){
visy[y] = true;
if(linker[y] == - || Find(linker[y])){
linker[y] = x;
return true;
}
}
else
{
slack[y] = min(slack[y], t);
}
}
return false;
} void KM()
{
///初始化
memset(linker, -, sizeof(linker));
memset(ey, , sizeof(ey)); for(int i = ; i <= N; i++){
ex[i] = a[i][];
for(int j = ; j <= N; j++){
ex[i] = max(ex[i], a[i][j]);
}
} for(int x = ; x <= N; x++){ fill(slack, slack++N, INF); while(){
memset(visx, , sizeof(visx));
memset(visy, , sizeof(visy));
if(Find(x)) break;
double min_slack = INF;
for(int i = ; i <= N; i++){
if(!visy[i])
min_slack = min(min_slack, slack[i]);
}
for(int i = ; i <= N; i++){
if(visx[i]) ex[i]-=min_slack;
}
for(int j = ; j <= N; j++){
if(visy[j]) ey[j]+=min_slack;
}
}
}
} //double dis(date n1, date n2)
//{
// double res = 0;
// res = sqrt((n1.x - n2.x)*(n1.x - n2.x) + (n1.y - n2.y)*(n1.y - n2.y));
// return -res;
//} int main()
{
while(~scanf("%d", &N)){
// init();
for(int i = ; i <= N; i++){
scanf("%lf%lf", &node1[i].x, &node1[i].y);
}
for(int i = ; i <= N; i++){
scanf("%lf%lf", &node2[i].x, &node2[i].y);
}
for(int i = ; i <= N; i++)
for(int j = ; j <= N; j++){
a[j][i] = -sqrt((node1[i].x - node2[j].x)*(node1[i].x - node2[j].x) + (node1[i].y - node2[j].y)*(node1[i].y - node2[j].y));
}
KM();
for(int i = ; i <= N; i++){
printf("%d\n", linker[i]);
}
}
return ;
}

POJ 3565 Ants 【最小权值匹配应用】的更多相关文章

  1. POJ 3565 Ants (最小权匹配)

    题意 给出一些蚂蚁的点,给出一些树的点,两两对应,使他们的连线不相交,输出一种方案. 思路 一开始没想到怎么用最小权匹配--后来发现是因为最小权匹配的方案一定不相交(三角形两边之和大于第三边)--还是 ...

  2. POJ-2195 Going Home---KM算法求最小权值匹配(存负边)

    题目链接: https://vjudge.net/problem/POJ-2195 题目大意: 给定一个N*M的地图,地图上有若干个man和house,且man与house的数量一致.man每移动一格 ...

  3. POJ 2195 Going Home 【二分图最小权值匹配】

    传送门:http://poj.org/problem?id=2195 Going Home Time Limit: 1000MS   Memory Limit: 65536K Total Submis ...

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

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

  5. ZOJ-2342 Roads 二分图最小权值覆盖

    题意:给定N个点,M条边,M >= N-1.已知M条边都有一个权值,已知前N-1边能构成一颗N个节点生成树,现问通过修改这些边的权值使得最小生成树为前N条边的最小改动总和为多少? 分析:由于计算 ...

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

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

  7. POJ 1797 Heavy Transportation(Dijkstra变形——最长路径最小权值)

    题目链接: http://poj.org/problem?id=1797 Background Hugo Heavy is happy. After the breakdown of the Carg ...

  8. POJ 2404 Jogging Trails(最小权完美匹配)

    [题目链接] http://poj.org/problem?id=2404 [题目大意] 给出一张图,求走遍所有的路径至少一次,并且回到出发点所需要走的最短路程 [题解] 如果图中所有点为偶点,那么一 ...

  9. hdu 1853 Cyclic Tour (二分匹配KM最小权值 或 最小费用最大流)

    Cyclic Tour Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/65535 K (Java/Others)Total ...

随机推荐

  1. spark第六篇:Spark Streaming Programming Guide

    预览 Spark Streaming是Spark核心API的扩展,支持高扩展,高吞吐量,实时数据流的容错流处理.数据可以从Kafka,Flume或TCP socket等许多来源获取,并且可以使用复杂的 ...

  2. 《The Python Standard Library》——http模块阅读笔记1

    官方文档:https://docs.python.org/3.5/library/http.html 偷个懒,截图如下: 即,http客户端编程一般用urllib.request库(主要用于“在这复杂 ...

  3. (转)Linux curl命令参数详解

    Linux curl命令参数详解 命令:curl在Linux中curl是一个利用URL规则在命令行下工作的文件传输工具,可以说是一款很强大的http命令行工具.它支持文件的上传和下载,是综合传输工具, ...

  4. 测试次数(C++)

    测试次数(结果填空) (满分17分) 注意事项:问题的描述在考生文件夹下对应题号的“题目.txt”中.相关的参考文件在同一目录中.请先阅读题目,不限解决问题的方式,只要求提交结果.必须通过浏览器提交答 ...

  5. storm中KafkaSpout的选择

    Storm最常用的消息源就是Kafka,在对接的时候大多需要使用KafkaSpout: 在网上大概有两种KafkaSpout,一种是只有几十行,一种却有一大啪啦类文件. 在kafka中,同一个part ...

  6. 数据段描述符和代码段描述符(二)——《x86汇编语言:从实模式到保护模式》读书笔记11

    这篇博文,我们编写一个C语言的小程序,来解析数据段或者代码段描述符的各个字段.这样我们阅读原书的代码就会方便一点,只要运行这个小程序,就可以明白程序中定义的数据段或者代码段的描述符了. 这段代码,我用 ...

  7. mysql 查两个表相同的值

    比如一个数据库 表A和表B 都有一个username字段, 现查出与表A中username值相同的表B的username和password数据 select B.username,B.password ...

  8. The eighteen day

    27th Nov 2018 Setting goals is the first step in turning the invisible into the visiable   ---Tony R ...

  9. 详解nodejs中使用socket的私聊和公聊的办法

    详解nodejs中使用socket的私聊和公聊的办法 nodejs的应用中,关于socket应该是比较出彩的了,socket.io在github上有几万人的star,它的成功应该是不输于express ...

  10. 深入理解Javascript之执行上下文(Execution Context)

    在这篇文章中,将比较深入地阐述下执行上下文 - Javascript中最基础也是最重要的一个概念.相信读完这篇文章后,你就会明白javascript引擎内部在执行代码以前到底做了些什么,为什么某些函数 ...