[置顶] Ants(Northeastern Europe 2007)
![]() |
|
||||
|
Ants
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 (− 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 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 Sample Output 4 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(Northeastern Europe 2007)的更多相关文章
- 在UWP中页面滑动导航栏置顶
最近在研究掌上英雄联盟,主要是用来给自己看新闻,顺便copy个界面改一下段位装装逼,可是在我copy的时候发现这个东西 当你滑动到一定距离的时候导航栏会置顶不动,这个特性在微博和淘宝都有,我看了@ms ...
- WinFrom窗体始终置顶
调用WindowsAPI使窗体始终保持置顶效果,不被其他窗体遮盖: [DllImport("user32.dll", CharSet = CharSet.Auto)] privat ...
- winform窗体置顶
winform窗体置顶 金刚 winform 置顶 今天做了一个winform小工具.需要设置置顶功能. 网上找了下,发现百度真的很垃圾... 还是必应靠谱些. 找到一个可以链接. https://s ...
- 自定义置顶TOP按钮
简述一下,分为三个步骤: 1. 添加Html代码 2. 调整Css样式 3. 添加Jquery代码 具体代码如下: <style type="text/css"> #G ...
- ahk之路:利用ahk在window7下实现窗口置顶
操作系统:win7 64位 ahk版本:autohotkey_L1.1.24.03 今天安装了AutoHotkey_1.1.24.03.SciTE.PuloversMacroCreator,重新开始我 ...
- Qt中让Qwidget置顶的方法
一般来是说窗体置顶和取消只要 setWindowFlags(Qt::WindowStaysOnTopHint); setWindowFlags(Qt::Widget); 要 ...
- js之滚动置顶效果
0.js获取高度 ? 1 2 3 4 5 6 document.all // 只有ie认识 document.body.clientHeight // 文档的高,屏幕 ...
- Javascript笔记----实现Page页面右下角置顶按钮.
从用博客开始,发现博客园中很多博友的博客中在Page右下角都有个图标,不论屏幕怎么拉伸,都始终停留在右下角.点击后页面置顶.后面想想写一个Demo来实现这种效果吧. 一. 图标右下角固定. 1.SS ...
- JavaScript学习笔记-元素在滚动条滑动一定高度后自动置顶
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...
随机推荐
- C语言的这些事情有关内存
C语言的程序内存布局,从高到低依次为:栈区.堆区.未初始化数据区.初始化数据区.代码区. 一.栈区 由编译器自己主动管理,无需程序猿手工控制.存放函数的參数值.局部变量的值等.栈区内容从高地址到低地址 ...
- 必要的软件架构师——编译原理·语法
最近软测试.我观看进程的视频! 发现里面有很多内容已经在自我不错的接触过程.而占80%比例! 但其中的一部分.我很奇怪的一部分.研究,在这里,将我研究的内容整理分享给大家! 编译原理: 首先,我第一眼 ...
- EasyUI 扩展自己定义EasyUI校验规则 验证规则(经常使用的)
比如 校验输入框仅仅能录入0-1000之间 最多有2位小数的数字 表单<input type="text" id="rate" name="ra ...
- 基于Jquery的多彩二维码的生成
Demo效果图: 源代码: @{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name=&quo ...
- ORA-00932: inconsistent datatypes: expected - got CLOB
从最近的数据库10.2.0.3升级到10.2.0.5之后,一些对象可以不被编译.查看这些对象的主列表不严格写入之前现在SQL这些语法结果package无法成功编译,诸如select查询列中不能使用混淆 ...
- Socket 学习(三).1 tcp 通讯
实现了,局域网客户端 对客户端 的通讯. 实际上这是 一个 客户端 兼 服务端 . 2个阿里云服务器测试 效果图: 本地效果图: using System; using System.Collecti ...
- IOS启动其他应用程序
从app1打开app2.主要的思路就是,能够为app2定义一个URL,在app1中通过打开这个URL来打开app2,在此过程中.能够传送一些參数. 在app1的代码中打开刚才定义的URL.代码例如以下 ...
- C语言库函数大全及应用实例七
原文:C语言库函数大全及应用实例七 [编程资料]C语言库函数大全及应用实例七 函数名: getw 功 能: 从流中取一整数 用 法: int getw(FILE *strem); 程序例: #i nc ...
- protobuf-net-data
protobuf-net http://www.codeproject.com/Articles/642677/Protobuf-net-the-unofficial-manual https://g ...
- ACM-光滑最小生成树project——hdu1863
***************************************转载请注明出处:http://blog.csdn.net/lttree************************** ...



