UVALive 4043 Ants
KM 构图求最小权值匹配
保证最小的权值,所连的边一定是能够不相交的.
| Time Limit: 3000MS | Memory Limit: Unknown | 64bit IO Format: %lld & %llu |
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
Input has several dataset. The first line of each dataset contains a single integer number n(1n
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(- 10000x, y
10000) 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
For each dataset, 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 i -th ant colony. Print a blank line between datasets.
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
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath> using namespace std; const int maxn=220;
const double INF=999999999999999.;
const double eps=1e-5; struct Dian
{
double x,y;
}white[maxn*maxn],black[maxn*maxn]; int n;
double g[maxn][maxn];
int linker[maxn*maxn];
double lx[maxn*maxn],ly[maxn*maxn];
double slack[maxn*maxn];
bool visx[maxn*maxn],visy[maxn*maxn]; bool dfs(int x)
{
visx[x]=true;
for(int y=0;y<n;y++)
{
if(visy[y]) continue;
double tmp=lx[x]+ly[y]-g[x][y];
if(fabs(tmp)<eps)
{
visy[y]=true;
if(linker[y]==-1||dfs(linker[y]))
{
linker[y]=x;
return true;
}
}
else if(slack[y]>tmp)
slack[y]=tmp;
}
return false;
} int KM()
{
memset(linker,-1,sizeof(linker));
memset(ly,0,sizeof(ly));
for(int i=0;i<n;i++)
{
lx[i]=-INF;
for(int j=0;j<n;j++)
if(g[i][j]>lx[i])
lx[i]=g[i][j];
}
for(int x=0;x<n;x++)
{
for(int i=0;i<n;i++)
slack[i]=INF;
while(true)
{
memset(visx,false,sizeof(visx));
memset(visy,false,sizeof(visy));
if(dfs(x)) break;
double d=INF;
for(int i=0;i<n;i++)
if(!visy[i]&&d>slack[i])
d=slack[i];
for(int i=0;i<n;i++)
if(visx[i])
lx[i]-=d;
for(int i=0;i<n;i++)
if(visy[i])
ly[i]+=d;
else
slack[i]-=d;
}
}
} double Dist(Dian a,Dian b)
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
} int main()
{
bool first=false;
while(scanf("%d",&n)!=EOF)
{
if(first) putchar(10);
else first=true;
for(int i=0;i<n;i++)
{
double a,b;
scanf("%lf%lf",&a,&b);
white[i]=(Dian){a,b};
}
for(int i=0;i<n;i++)
{
double a,b;
scanf("%lf%lf",&a,&b);
black[i]=(Dian){a,b};
}
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
double t=Dist(black[i],white[j]);
g[i][j]=-t;
}
}
KM();
for(int i=0;i<n;i++)
{
printf("%d\n",linker[i]+1);
}
}
return 0;
}
UVALive 4043 Ants的更多相关文章
- UVALive 4043 Ants 蚂蚁(二分图最佳完美匹配,KM算法)
题意: 有n个蚂蚁n棵树,蚂蚁与树要配对,在配对成功的一对之间连一条线段,要求所有线段不能相交.按顺序输出蚂蚁所匹配的树. 思路: 这个题目真是技巧啊,不能用贪心来为每个蚂蚁选择最近的树,这样很可能是 ...
- UVALive 4043 Ants(二分图完美匹配)
题意:每个蚁群有自己的食物源(苹果树),已知蚂蚁靠气味辨别行进方向,所以蚁群之间的行动轨迹不能重叠.现在给出坐标系中n个蚁群和n棵果树的坐标,两两配对,实现以上要求.输出的第 i 行表示第 i 个蚁群 ...
- UVaLive 4043 Ants (最佳完美匹配)
题意:给定 n 个只蚂蚁和 n 棵树的坐标,问怎么匹配使得每个蚂蚁到树的连线不相交. 析:可以把蚂蚁和树分别看成是两类,那么就是一个完全匹配就好,但是要他们的连线不相交,那么就得考虑,最佳完美匹配是可 ...
- Uvalive 4043 Ants —— 二分图最大权匹配 KM算法
题目链接:https://vjudge.net/problem/UVALive-4043 题意: 给出n个白点和n个黑点的坐标, 要求用n条不相交的线段把他们连接起来,其中每条线段恰好连接一个白点和黑 ...
- 训练指南 UVALive - 4043(二分图匹配 + KM算法)
layout: post title: 训练指南 UVALive - 4043(二分图匹配 + KM算法) author: "luowentaoaa" catalog: true ...
- LA - 4043 - Ants
题意:n只蚂蚁,n棵树,每只蚂蚁要连一棵树,连线(直线)不能相交,给出n只蚂蚁和n棵树的坐标,输出n只蚂蚁所配对的树的编号(1 <= n <= 100, -10000 <= 坐标x, ...
- UVALive 4043 转化最佳完美匹配
首先黑点和白点是组成一个二分图这毫无疑问 关键是题目中要求的所有黑白配的线不能交叉...一开始我也没想到这个怎么转化为二分图里面的算法. 后来看书才知道,如果两两交叉,则可以把两根线当四边形的对角线, ...
- UVALive 7505 Hungry Game of Ants (2015Ecfinal)
题意: 长度是n的线段上点的编号从1~n,每个点有一只蚂蚁蚂蚁的体重等于该点的编号,最初每只蚂蚁可以选择向右走或者向左走两只蚂蚁相遇时体重大的吃掉体重小的并且体重增加为两只的体重和,走到边界时掉头,问 ...
- UVa 12709 && UVaLive 6650 Falling Ants (水题)
题意:给定 n 个长方体的长,宽,高,让你求高最大的时候体积最大是多少. 析:排序,用高和体积排序就好. 代码如下: #pragma comment(linker, "/STACK:1024 ...
随机推荐
- JCo 指南
http://blog.csdn.net/asdfak/article/details/5834731 JAVA 调用SAP端接口 Java Connector and BAPI 前些日子想去深入的研 ...
- 使用jQuery操作元素的属性与样式
本文学习如何使用jQuery获取和操作元素的属性和CSS样式. 元素属性和Dom属性 对于下面这样一个标签元素: <img id='img' src="1.jpg" alt= ...
- POJ 3045 Cow Acrobats (贪心)
POJ 3045 Cow Acrobats 这是个贪心的题目,和网上的很多题解略有不同,我的贪心是从最下层开始,每次找到能使该层的牛的风险最小的方案, 记录风险值,上移一层,继续贪心. 最后从遍历每一 ...
- CentOS(Linux) - SVN使用笔记(二) - 创建SVN仓库及下载仓库到本地
1.安装: 参考文章 CentOS(Linux) - SVN使用笔记(一) - 安装SVN过程及开启和关闭svn服务指令 2.创建仓库 #创建项目目录 mkdir /usr/svn#进入目录cd / ...
- 17--Box2D使用(三、触摸交互)
Box2D引擎与触摸的交互通过创建鼠标关节以及碰撞检测来得到触摸点下面的刚体,在根据触摸操作完成相应的功能.首先添加触摸响应函数声明 virtual void ccTouchesBegan(cocos ...
- js拖动层
模仿网易彩票网(http://caipiao.163.com/)的登陆框自己做了一个拖动层,不过有点小问题——在谷歌浏览拖动的时候鼠标状态变成了文字状态(cursor:text;) <!DOCT ...
- PHP学习系列(1)——字符串处理函数(1)
从09年开始使用JAVA到现在差不多5年多了,然后由于即将要去的公司(研究僧终于要毕业了!)是使用PHP,Python作为后台开发语言的,所以要开始一段双P的学习旅程.用过PHP的都说这是很简单的一门 ...
- python之7-3对象的信息/方法获取
我们可以用dir()来获取一个类,一个模块,一个字符串的信息/方法 例如: #可以列出Image模块的方法 >>import Image >>dir(Image) #列出字符串 ...
- hadoop集群的故障概率估算
hadoop集群的机器数业界(国内)最大的在5000左右,是什么限制了集群的规模呢?有好几个原因. 1. namenode的内存大小限制 2. 机器故障概率随着机器数目增大而增大,通常一份数据存储在h ...
- hdu 1385 Minimum Transport Cost
http://acm.hdu.edu.cn/showproblem.php?pid=1385 #include <cstdio> #include <cstring> #inc ...