题目链接:点击打开链接

题意:

给定二维坐标上的4个点

问:

找一个点使得这个点距离4个点的距离和最小

输出距离和。

思路:

若4个点不是凸4边形。则一定是端点最优。

否则就是2条对角线的交点最优,能够简单证明一下。

对于凸4边形则先极角排序一下。

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
typedef double ll;
const int N = 5;
int n = 4;
double x[N], y[N]; struct Point {
ll x, y, dis;
} s[4], p0;
ll Dis(ll x1, ll y1, ll x2, ll y2)
{
return (x1-x2)*(x1-x2)+(y1-y2)*(y1-y2);
}
int Cmp_PolarAngel(struct Point p1, struct Point p2, struct Point pb)
{
double delta=(p1.x-pb.x)*(p2.y-pb.y)-(p2.x-pb.x)*(p1.y-pb.y);
if (delta<0.0) return 1;
else if (delta==0.0) return 0;
else return -1;
}
bool Is_LeftTurn(struct Point p3, struct Point p2, struct Point p1)
{
int type=Cmp_PolarAngel(p3, p1, p2);
if (type<0) return true;
return false;
}
int Cmp(const void*p1, const void*p2)
{
struct Point*a1=(struct Point*)p1;
struct Point*a2=(struct Point*)p2;
int type=Cmp_PolarAngel(*a1, *a2, p0);
if (type<0) return -1;
else if (type==0) {
if (a1->dis<a2->dis) return -1;
else if (a1->dis==a2->dis) return 0;
else return 1;
}
else return 1;
}
double cal(double x1, double y1, double x2, double y2) {
return sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));
}
int main() {
while (~scanf("%lf%lf%lf%lf%lf%lf%lf%lf", &x[0], &y[0], &x[1], &y[1], &x[2], &y[2], &x[3], &y[3])) {
if(x[0] == -1) break;
double ans = 1e10, di;
for(int i = 0; i < n; i ++) {
di = 0.0;
for(int j = 0; j < n; j ++) {
if(j == i) continue;
di += cal(x[i], y[i], x[j], y[j]);
}
ans = min(ans, di);
} double xx = x[0] + x[1] + x[2] + x[3];
double yy = y[0] + y[1] + y[2] + y[3];
di = 0.0;
for(int j = 0; j < n; j ++) {
di += cal(xx/4, yy/4, x[j], y[j]);
}
ans = min(ans, di); p0.x = x[0], p0.y = y[0];
for(int i = 0; i < 4; i ++) {
s[i].x = x[i];
s[i].y = y[i];
}
for(int i = 0; i < n; i ++) {
s[i].dis = cal(s[0].x, s[0].y, s[i].x, s[i].y);
}
qsort(s+1, n-1, sizeof(struct Point), Cmp); x[0] = s[0].x; y[0] = s[0].y;
x[1] = s[2].x; y[1] = s[2].y;
x[2] = s[1].x; y[2] = s[1].y;
x[3] = s[3].x; y[3] = s[3].y; double k1 = (y[0] - y[1]) / (x[0] - x[1]);
double k2 = (y[3] - y[2]) / (x[3] - x[2]);
double ansx, ansy;
if(x[0] == x[1]) {
ansx = x[0];
if(x[2] == x[3]) {
ansy = yy / 4;
} else {
ansy = k2 * (ansx - x[2]) + y[2];
}
} else {
if(x[2] == x[3]) {
ansx = x[2];
ansy = k1 * (ansx - x[1]) + y[1];
} else {
if(k1 != k2) {
ansx = (y[2] - y[1] + k1*x[1] - k2*x[2]) / (k1 - k2);
ansy = k1*(ansx - x[1]) + y[1];
} else {
ansx = 1000;
ansy = 1000;
}
}
}
di = 0.0;
for(int j = 0; j < n; j ++) {
di += cal(ansx, ansy, x[j], y[j]);
}
ans = min(ans, di); printf("%.4f\n", ans);
}
return 0;
}

UVALive 5102 Fermat Point in Quadrangle 极角排序+找距离二维坐标4个点近期的点的更多相关文章

  1. C语言 数组输出,冒泡排序法,沉底排序法,二维数组输出,输出字母列长度,从随机数组中找重复数

    #include <stdio.h> #define sum 3+4//宏定义是原封不动的使用used for test4 #include <time.h>//used fo ...

  2. [java学习笔记]java语言基础概述之数组的定义&常见操作(遍历、排序、查找)&二维数组

    1.数组基础 1.什么是数组:           同一类型数据的集合,就是一个容器. 2.数组的好处:           可以自动为数组中的元素从零开始编号,方便操作这些数据. 3.格式:  (一 ...

  3. 二维坐标点排序(JavaScript)

    今天给大家分享下最近web项目中出现的一个技术难点问题--坐标排序: 如下图所示,要求在前端页面上按顺序将下面5个模块的坐标依次保存至数据库 现在已知信息如下: 1.每个模块分别为一个div 2.每个 ...

  4. C# 实现二维数组的排序算法(代码)

    class Order { /// <summary> /// 对二维数组排序 /// </summary> /// <param name="values&q ...

  5. UVaLive 4064 Magnetic Train Tracks (极角排序)

    题意:给定 n 个不三点共线的点,然后问你能组成多少锐角或者直角三角形. 析:可以反过来求,求有多少个钝角三角形,然后再用总的减去,直接求肯定会超时,但是可以枚举每个点,以该点为钝角的那个顶点,然后再 ...

  6. POJ 1696 Space Ant 【极角排序】

    题意:平面上有n个点,一只蚂蚁从最左下角的点出发,只能往逆时针方向走,走过的路线不能交叉,问最多能经过多少个点. 思路:每次都尽量往最外边走,每选取一个点后对剩余的点进行极角排序.(n个点必定能走完, ...

  7. Space Ant---poj1696(极角排序)

    题目链接:http://poj.org/problem?id=1696 题意:给你n个点,然后我们用一条线把它们连起来,形成螺旋状: 首先找到左下方的一个点作为起点,然后以它为原点进行极角排序,找到极 ...

  8. poj2280Amphiphilic Carbon Molecules(极角排序)

    链接 卡了几天的破题,对于hdu的那份数据,这就一神题.. 借助极角排序,枚举以每一个点进行极角排序,然后构造两条扫描线,一个上面一个下面,两条同时走,把上线和下线的点以及上线左边的点分别统计出来,如 ...

  9. LightOJ 1285 - Drawing Simple Polygon (几何,极角排序)

    1285 - Drawing Simple Polygon   PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: ...

随机推荐

  1. jbpmAPI-3

    第三章.jBPM安装程序 3.1 .先决条件这个脚本假设您具备Java JDK 1.6 +(设置JAVA_HOME),和Ant 1.7 +安装.如果你没有,请使用以下链接下载并安装:Java:http ...

  2. python自定义排序函数

    Python内置的 sorted()函数可对list进行排序: >>>sorted([36, 5, 12, 9, 21]) [5, 9, 12, 21, 36] 但 sorted() ...

  3. android 子线程更新UI

    参考http://examples.javacodegeeks.com/android/core/os/handler/android-handler-example/package com.exam ...

  4. jQuery.ui autoComplete使用

    官网  http://api.jqueryui.com/autocomplete/#option-source 参考了 http://www.cnblogs.com/lwme/archive/2012 ...

  5. 新唐M0特点分析

    1,价格低,05x系列0.6-1.5美金,1xx系列1.5-3.5美金:2,性能好,最新32位CORTEX-M0的ARM核,唯一可工作到+5.5V的CORTEX-M0:3,速度快,CPU核能跑到50M ...

  6. Android 百度地图开发问题----解决地图有时候加载不出来问题

    相信很多人在开发百度地图的时候会出现百度地图有时候会加载不出来,只显示网格图. 这个问题究其原因就是申请百度key的时候填写的SHA1也就是指纹证书有问题.估计很多开发者都是照着百度开放平台上介绍的流 ...

  7. linux技术框架

    编程语言 一般使用c或者c++ linux使用 鸟哥私房菜 工具使用 代码编辑source insight,代码编译gcc,代码调试gdb,代码编译组织makefile,命令执行shell,文本编辑n ...

  8. DllMain加载其他DLL造成的死锁问题及其解决办法

    使用VS 2008新建一个MFC ActiveX工程,因为在工程里要用到GDI+.我习惯把初始化GDI+库的代码放在应用程序类的InitInstance函数,对应的销毁代码放在ExitInstance ...

  9. HDU 1276 士兵队列训练问题

    模拟题,学了一下list it=li.erase(it):指向删除后的第一个元素 #include <cstdio> #include <list> using namespa ...

  10. hdu 3966 Aragorn&#39;s Story(树链剖分+树状数组)

    pid=3966" target="_blank" style="">题目链接:hdu 3966 Aragorn's Story 题目大意:给定 ...