|
|
Ants
Time Limit: 5000MS |
|
Memory Limit: 65536K |
Total Submissions: 3539 |
|
Accepted: 1064 |
|
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 ofn ant colonies andn 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 alln 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 numbern (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 coordinatesx andy (−<nobr>10 000</nobr> ≤x,y ≤<nobr>10 000</nobr>) 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 filen lines with one integer number on each line. The number written oni-th line denotes the number (from 1 ton) of the apple tree that is connected
to thei-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代表蚂蚁军团和苹果树各有n个。然后叫你通过计算而确定如何连接。
从题目中可以看到,苹果树和蚂蚁分别分成了两个部分。因此,可以轻松的想到了二分匹配问题。
然后就可以根据数学几何的知识,如果a1--b1与a2--b2相交,那么dist(a1,b1)+dist(a2,b2)一定大于dist(a1,b2)+dist(a2,b1),因为三角形的两边之和大于第三边。因此可得知最佳匹配中不会出现线段相交的情况。
还要注意输出的时候,因为是树按照从1--n输出的所以要进行转换后再输出。
题目意思如果知道了会,但不知细节问题的话,那就看下面的两个代码吧。
一个是O(n^4)的KM( ),(还有一个是经过松弛处理的KM();不过非本人原创)。
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cmath>
#define CL(x,v);memset(x,v,sizeof(x));
#define maxn 105
#define INF 1000000
using namespace std;
int Left[maxn],n;
bool S[maxn],T[maxn];
double Lx[maxn],Ly[maxn],w[maxn][maxn];
struct Point
{
double x,y;
}p[maxn*2];
double Dist(Point a,Point b)
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
bool match(int i)
{
S[i] = true;
for(int j = 1;j <= n;j++)
if(fabs(Lx[i]+Ly[j]-w[i][j])<1e-5&&!T[j])
{
T[j] = true;
if(!Left[j]||match(Left[j])){
Left[j] = i;
return true;
}
}
return false;
}
// 没有这个函数,则就是稳定婚姻模型了。
void update()
{
double a = INF;
for(int i = 1;i <= n;i++) if(S[i])
for(int j = 1;j <= n;j++) if(!T[j])
a = min(a,Lx[i]+Ly[j]-w[i][j]);
for(int i = 1;i <= n;i++){
if(S[i]) Lx[i] -= a;
if(T[i]) Ly[i] += a;
}
}
void KM()
{
CL(Left,0);CL(Lx,0);CL(Ly,0);
for(int i = 1;i <= n;i++)
for(int j = 1;j <= n;j++)
Lx[i] = max(Lx[i],w[i][j]);
for(int i = 1;i <= n;i++){
while(1){
CL(S,0); CL(T,0);
if(match(i)) break;
else update();
}
}
}
int main()
{
while(cin>>n)
{
for(int i = 1;i <= 2*n;i++)
cin>>p[i].x>>p[i].y;
for(int i = 1;i <= n;i++)
for(int j = 1;j <= n;j++)
w[i][j] = -Dist(p[i],p[j+n]); //这里为嘛?加一个负号。
KM(); //哪位大牛,大神知道的请告诉一下哈
int tree[maxn];
for(int i = 1;i <= n;i++)
tree[Left[i]] = i;
for(int i = 1;i <= n;i++)
cout<<tree[i]<<endl;
}
return 0;
}
分享到:
相关推荐
ants metaheuristic methods
配准ANTs工具的官方介绍文档
ANTS Profiler 5.1.0.15 注册机,可注册ANTS全系列产品。
ants代码性能检查工具
好东西啊,竟然能用,抓紧下啊, 自己测试过,可以使用。
ANTs-2.3.1.zip ANTS2.3.1版本安装包 直接下载解压,亲测可用。
我知道找这个不容易,我过过了可用. %WinDir%\system32\drivers\etc\hosts 127.0.0.1 www.reflector.net 127.0.0.1 licensing.red-gate.com 127.0.0.1 update.red-gate.com
ants 工具箱 学习的帮手 你会有用的
Ants Performance Profiler 8 最新版,包含Performance和Profiler两个工具,仅供学习使用
font called ants valley
ants-go - 开源分、布式、restful golang爬虫引擎
Redgate ANTS Profiler 4.0.0.861
很好用的内存监测工具
ANTS Memory Profile 是一款使用方便,可以很快看到内存使用情况的分析工具,对内存泄漏有很好的监视功能!
好用的.NET平台的检测内存工具,可离线注册
Ant入门讲解,有视频和源码
Ants_黑蚂蚁是一个EXE文件,打开屏幕会出现蚂蚁,可以攻击蚂蚁,是一个玩的东西啊! 鉴于此,也希望大家按此说明研究软件!谢谢
医学图像处理matlab软件包,在matlab平台下,处理医学图像配准分割的常用软件包指南
资源分类:Python库 所属语言:Python 使用前提:需要解压 资源全名:ants_client-1.7b1-py2-none-any.whl 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
matlab蚁群工具箱 具体应用需要自己修改