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 ...
随机推荐
- [實現DDD] 第10章 聚合(1)設計原則
聚合只是將一些實體(Entity)與值對象(Value Object)聚集起來的對象樹嗎?? 有些途徑可能使我們設計出不正確的聚合模型, 如:可能為了對象組合上的方便而將聚合設計的很大;也可能設計的聚 ...
- multiprocessing 模块
multiprocessing模块 进程对象 创建 p = Process(target=foo, args=(param,)) 属性 p.daemon: True为守护进程, 守护进程内无法再开启子 ...
- LaTex 2
LaTex 入门 此时是否安装成功 如果安装成功了LaTeX, 那么在计算机上会多出来LaTeX的编译器, LaTex Live 安装包在计算机上安装了多个不同的编译器, 有latex, xelate ...
- HRBUST 1909——理工门外的树——————【离线处理,差分前缀和】
理工门外的树 Time Limit: 1000 MS Memory Limit: 32768 KB 64-bit integer IO format: %lld , %llu Java class n ...
- Python sh模块--------替换subprocess的利器
官方文档有句话"allows you to call any program",并且: helps you write shell scripts in Python by giv ...
- 深入理解JavaScript系列(10):JavaScript核心(晋级高手必读篇)
本篇是ECMA-262-3 in detail系列的一个概述(本人后续会翻译整理这些文章到本系列(第11-19章).每个章节都有一个更详细的内容链接,你可以继续读一下每个章节对应的详细内容链接进行更深 ...
- 浅谈 .NET Framework 与 .NET Core 的区别与联系
2017到了,咱们学点啥啊,要想知道学点啥,先弄清.NET Framework 与 .NET Core 这两个概念 .当今 net 生态系统如下: 从上面图中我们可以看到.net 主要分为三个部分 ...
- 面向对象(基础oop)之类与对象
大家好,我叫李京阳,,很高兴认识大家,之所以我想开一个自己的博客,就是来把自己所了解的知识点通过自己的话写一下,希望被博客园的朋友们点评和一起讨论一下,也希望从博客园中多认识一些软件开发人员!现在我开 ...
- C++里创建 Trie字典树(中文词典)(二)(插入、查找、导入、导出)
萌新做词典第二篇,做得不好,还请指正,谢谢大佬! 做好了插入与遍历功能之后,我发现最基本的查找功能没有实现,同时还希望能够把内存的数据存入文件保存下来,并可以从文件中导入词典.此外,数据的路径是存在配 ...
- Tomcat部分操作
一 概述 1.Tomcat是什么? Tomcat是Apache软件基金会提供的开源免费的服务器,适用于中小型系统与并发访问用户不是很多的情况. 2.域名 IP是互联网上一台计算机的唯一标识,但IP不容 ...