POJ 3565 Ants 【最小权值匹配应用】
传送门:http://poj.org/problem?id=3565
| 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 ≤ x, y ≤ 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 【最小权值匹配应用】的更多相关文章
- POJ 3565 Ants (最小权匹配)
题意 给出一些蚂蚁的点,给出一些树的点,两两对应,使他们的连线不相交,输出一种方案. 思路 一开始没想到怎么用最小权匹配--后来发现是因为最小权匹配的方案一定不相交(三角形两边之和大于第三边)--还是 ...
- POJ-2195 Going Home---KM算法求最小权值匹配(存负边)
题目链接: https://vjudge.net/problem/POJ-2195 题目大意: 给定一个N*M的地图,地图上有若干个man和house,且man与house的数量一致.man每移动一格 ...
- POJ 2195 Going Home 【二分图最小权值匹配】
传送门:http://poj.org/problem?id=2195 Going Home Time Limit: 1000MS Memory Limit: 65536K Total Submis ...
- POJ 3565 Ants(最佳完美匹配)
Description Young naturalist Bill studies ants in school. His ants feed on plant-louses that live on ...
- ZOJ-2342 Roads 二分图最小权值覆盖
题意:给定N个点,M条边,M >= N-1.已知M条边都有一个权值,已知前N-1边能构成一颗N个节点生成树,现问通过修改这些边的权值使得最小生成树为前N条边的最小改动总和为多少? 分析:由于计算 ...
- poj3565 Ants km算法求最小权完美匹配,浮点权值
/** 题目:poj3565 Ants km算法求最小权完美匹配,浮点权值. 链接:http://poj.org/problem?id=3565 题意:给定n个白点的二维坐标,n个黑点的二维坐标. 求 ...
- POJ 1797 Heavy Transportation(Dijkstra变形——最长路径最小权值)
题目链接: http://poj.org/problem?id=1797 Background Hugo Heavy is happy. After the breakdown of the Carg ...
- POJ 2404 Jogging Trails(最小权完美匹配)
[题目链接] http://poj.org/problem?id=2404 [题目大意] 给出一张图,求走遍所有的路径至少一次,并且回到出发点所需要走的最短路程 [题解] 如果图中所有点为偶点,那么一 ...
- hdu 1853 Cyclic Tour (二分匹配KM最小权值 或 最小费用最大流)
Cyclic Tour Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/65535 K (Java/Others)Total ...
随机推荐
- Python+Selenium-BDD框架之behave
(查看behave具体教程可以访问官网: http://pythonhosted.org/behave/) 1.安装behave 安装好python后,使用 pip install behave命令安 ...
- myeclipse更改后台代码不用重启tomcat的方法
myeclipse更改后台代码不用重启tomcat的方法 方法1:在WebRoot下的META-INF文件夹中新建一个名为context.xml文件,里面添加如下内容(要区分大小写): <C ...
- java中创建User Libray
第一步:右键项目==>Build Path ==>Configure Build Path... 第二步:选择Libraries==>点击 Add Library.. 第三步:选择U ...
- STM32F407使用MFRC522射频卡调试及程序移植成功
版权声明:转载请注明出处,谢谢 https://blog.csdn.net/Kevin_8_Lee/article/details/88865556 或 https://www.cnblogs.co ...
- 解决VMWARE 虚拟机安装64位系统“此主机支持 Intel VT-x,但 Intel VT-x 处于禁用状态
VMWARE WORKSTATION 在安装64为操作系统报错,报错内容如图: 错误提示已经很清楚了,需要在BIOS 中打开intel VT-x g功能,开启此功能的前提是: 1.首先要确定的就是你的 ...
- Javascript怎么跳出循环,嵌套循环。
今天要实现一个功能,在数组a中的每一项,对应数组b中的每一项,如果对应上了就给数组b的checked增加ture属性,如果查找不到就给数组b的checked增加false属性. 如果有哪里写的不对欢迎 ...
- jquery的$.getScript在IE下的缓存问题
jquery的$.getScript在IE下的缓存问题
- js之变量介绍
变量提升 JavaScript的函数定义有个特点,它会先扫描整个函数体的语句,把所有申明的变量“提升”到函数顶部: 'use strict'; function foo() { var x = 'He ...
- Spring MVC+Fastjson之时间类型序列化
时间类型序列化: 注意红色代码,必须引入fastjson的JSONField类,而非其它. import com.alibaba.fastjson.annotation.JSONField; @Ent ...
- 移除script标签引起的兼容性问题
一.应用场景: 有时候我们需要动态创建script标签实现脚本的按需加载,我们会为script标签绑定onload或者onreadystatechange事件,用于检测动态脚本是否加载并执行完毕,在事 ...