题目链接:点击打开链接

题意:

给定二维坐标上的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. Unity Editor下对资源进行操作时调用AssetModificationProcessor

    public class Test : UnityEditor.AssetModificationProcessor { private static void OnWillCreateAsset(s ...

  2. BZOJ 1770: [Usaco2009 Nov]lights 燈( 高斯消元 )

    高斯消元解xor方程组...暴搜自由元+最优性剪枝 -------------------------------------------------------------------------- ...

  3. 获取json格式字符串的简单方法

    有的时候需要找一些Json格式的字符串,可以打开任意一个网页进入到调试模式,然后看network相关的访问信息,就可以获取到. 比如: 在记笔记的时候,点击保存后,会发出一些请求,然后有相应的相应,任 ...

  4. MyEclipse 在loading workbench 启动卡死

    解决方法: 找到Workspace目录,在.metadata(Mac 下是在 .metadata/.plugins)中删掉以下两个文件 org.eclipse.ui.workbench org.ecl ...

  5. 初识python yield

    for sel in response.xpath('//ul/li'): item = DmozItem() item['title'] = sel.xpath('a/text()').extrac ...

  6. Android UiAutomator 自动化测试编译运行---新手2

    1.首先打开eclipse创建java项目

  7. Django里面的RequestContext

    c = RequestContext(request, { 'foo': 'bar', }) get_template('about.html').render(c) 当我们定义一个RequestCo ...

  8. windows上放弃使用PyGTK

    windows上放弃使用PyGTK - riag的专栏 - 博客频道 - CSDN.NET windows上放弃使用PyGTK 分类: python 2010-03-31 16:47 1054人阅读 ...

  9. android使用全局变量的两种方法

         在我们使用android编写程序的时候,少不了想利用全局变量,但是面向对象语言和过程语言区别很大,不再是include就可以的.这里我写了使用全局变量的两种方法: 1.使用applicati ...

  10. Palindrome(Manacher)

    Palindrome Time Limit: 15000MS   Memory Limit: 65536K Total Submissions: 6183   Accepted: 2270 Descr ...