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 ...
随机推荐
- TCP协议三次握手、四次挥手过程
本文通过图来梳理TCP-IP协议相关知识.TCP通信过程包括三个步骤:建立TCP连接通道,传输数据,断开TCP连接通道.如图1所示,给出了TCP通信过程的示意图. 上图主要包括三部分:建立连接.传输数 ...
- Microsoft office 2016 for Mac 破解版下载安装
原文地址:https://www.cnblogs.com/liyan-blogs/p/5498293.html 1. 下载 office 到我网盘下载Microsoft office 2016 for ...
- zookeeper JAVA API 简单操作
package org.admln.program.Zoo_Test; import java.io.IOException; import java.security.NoSuchAlgorithm ...
- this,super,和继承
this是指当前对象的引用,super是指直接父类的引用 比如 我建造一个类 public class Person(){ private String name; private int age; ...
- 腾讯蔡晨:十年沉淀,腾讯iOA为企业安全保驾护航
欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 背景:5月23-24日,以"焕启"为主题的腾讯"云+未来"峰会再广州召开,广东省各级政府机构领导.海 ...
- HDU 1800——Flying to the Mars——————【字符串哈希】
Flying to the Mars Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- MySQL -U防止人为误操作
在很多时候操作数据库的时候,可能领导或DBA登陆了数据库,在执行update和delete时,忘记了加where,可能会导致清空表的悲剧,所以-U的好处就体现了. 1.mysql -U的帮助说明 -U ...
- Mysql只Union用法
MYSQL中的UNION UNION在进行表链接后会筛选掉重复的记录,所以在表链接后会对所产生的结果集进行排序运算,删除重复的记录再返回结果. 举例说明: select * from table1 u ...
- 【Sql server: T-Sql 技术内幕 系列】之索引篇
本文系 T-Sql技术内幕系列读后感. 用过数据库的程序猿都知道,索引可以极大的优化sql语句的执行时间,但是您要问我,怎么减少的,我只能说:"抱歉,我也不太清楚." 带着这个疑惑 ...
- 写C#代码时用到的中文简体字 、繁体字 对应的转化 (收藏吧)
简体字 下面有与之对应的繁体字 private const String Jian = "啊阿埃挨哎唉哀皑癌蔼矮艾碍爱隘鞍氨安俺按暗岸胺案肮昂盎凹敖熬翱袄傲奥懊澳芭捌扒叭吧笆疤巴拔跋靶 ...