传送门: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. 《The Python Standard Library》——http模块阅读笔记1

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

  2. windows删除指定日期前的文件

    @ echo offforfiles /p .\ /s /m 2008*.* /d -7 /c "cmd /c echo @file>>.\del.txt"forfil ...

  3. layer弹出层显示在top顶层

    父页面 导入 layer.js 或者 layui.all.js,导入后就能正常显示在父窗口页面区域. 1.显示在顶层窗口 top.layer.open({ type: 2, area: ['98%', ...

  4. ansile 命令解释选项

    1, -a MODULE_ARGS --args=MODULE_ARGS 作用传递参数给模块使用 2, --ask-vault-pass 执行时询问vault的密码 3, -B SECONDS --b ...

  5. (转)Shell——基本运算符

    Shell 基本运算符 原文:http://blog.csdn.net/sinat_36053757/article/details/70319481 Shell 和其他编程语言一样,支持多种运算符, ...

  6. Smart3D基础理论

    目录: 1. Smart3D发展进程 2. 硬件要求与建模原理 3. Smart3D建模优势 4.Smart3D的应用领域 5. Smart3D的软件组成 6. Samrt3D主控台概述 1. Sma ...

  7. java.lang.Exception: DEBUG -- CLOSE BY CLIENT STACK TRACE 的理解

    [2013-12-06 11:06:21,715] [C3P0PooledConnectionPoolManager[identityToken->2tl0n98y1iwg7cbdzzq7a|7 ...

  8. js面向对象2

    1.发展史 面向机器 面向过程:将程序的执行分解成若干个步骤 面向对象:将程序的执行分解成若干个事物 2.面向对象两个基本概念 类:代表某类事物,是抽象的 对象:代表某个事物,是具体的 3.快速入门 ...

  9. Unity 基础

    Unity 基础是unity入门的关键.他将讲解Unity的界面, 菜单项,使用资源,创设场景,并发布版本. 当你读完这段,你将理解unity是怎么工作的,如何有效地使用它,并且完成一个基本的游戏. ...

  10. 8、列表:ion-list

    1.基本样式 no-lines 属性 隐藏列表项之间的分割符 inset 属性 去掉 ion-list的 外边框. 默认 的 ion-list 是有外边框的.   /* ---示例代码----*/ & ...