Electric Fences
Kolstad & Schrijvers

Farmer John has decided to construct electric fences. He has
fenced his fields into a number of bizarre shapes and now must find
the optimal place to locate the electrical supply to each of the
fences.

A single wire must run from some point on each and every fence
to the source of electricity. Wires can run through other fences
or across other wires. Wires can run at any angle. Wires can run
from any point on a fence (i.e., the ends or anywhere in between)
to the electrical supply.

Given the locations of all F (1 <= F <= 150) fences (fences
are always parallel to a grid axis and run from one integer gridpoint
to another, 0 <= X,Y <= 100), your program must calculate
both the total length of wire required to connect every fence to
the central source of electricity and also the optimal location for
the electrical source.

The optimal location for the electrical source might be anywhere
in Farmer John's field, not necessarily on a grid point.

PROGRAM NAME: fence3

INPUT FORMAT

The first line contains F, the number of fences.
F subsequent
lines each contain two X,Y pairs each of which denotes the endpoints
of a fence.

SAMPLE INPUT (file fence3.in)

3
0 0 0 1
2 0 2 1
0 3 2 3

OUTPUT FORMAT

On a single line, print three space-separated floating point numbers, each with a single decimal place. Presume that your computer's output library will round the number correctly.

The three numbers are:

  • the X value of the optimal location for the electricity,
  • the Y value for the optimal location for the electricity, and
  • the total (minimum) length of the wire required.

SAMPLE OUTPUT (file fence3.out)

1.0 1.6 3.7


题意:给出N条平行于坐标轴的线段,要求一点,使得该点到所有线段之和最小。

由于精度要求很低,只要保留一位小数,所以可以随便乱搞,我就是在整数范围内先暴力找到一个满足题意的点,然后在x±1,y±1范围内暴力找到小数点后一位,最坏的情况需要暴力100*100*150,也是完全可以承受的。

暴力小数位的时候要注意精度问题,另外三分搜索也可以做。

Executing...
   Test 1: TEST OK [0.014 secs, 3380 KB]
   Test 2: TEST OK [0.016 secs, 3380 KB]
   Test 3: TEST OK [0.049 secs, 3380 KB]
   Test 4: TEST OK [0.024 secs, 3380 KB]
   Test 5: TEST OK [0.070 secs, 3380 KB]
   Test 6: TEST OK [0.046 secs, 3380 KB]
   Test 7: TEST OK [0.043 secs, 3380 KB]
   Test 8: TEST OK [0.051 secs, 3380 KB]
   Test 9: TEST OK [0.057 secs, 3380 KB]
   Test 10: TEST OK [0.046 secs, 3380 KB]
   Test 11: TEST OK [0.049 secs, 3380 KB]
   Test 12: TEST OK [0.076 secs, 3380 KB] All tests OK.
 /*
LANG:C++
TASK:fence3
*/ #include <iostream>
#include <cmath>
#include <stdio.h>
using namespace std;
#define X first
#define Y second typedef pair<double,double> Point;
int n; struct Fence
{
Point p1;
Point p2;
}fences[]; double dist_point(Point p1,Point p2)
{
return sqrt((double)(p1.X-p2.X)*(p1.X-p2.X)+(p1.Y-p2.Y)*(p1.Y-p2.Y));
} bool isIn(Fence f,Point p)
{
if(f.p1.X==f.p2.X) // 垂直方向
return f.p1.Y<=p.Y && p.Y<=f.p2.Y;
if(f.p1.Y==f.p2.Y) // 水平方向
return f.p1.X<=p.X && p.X<=f.p2.X;
} // 计算p点到各篱笆的距离
double dist(Point p)
{
double ans=;
for(int i=;i<n;i++)
{
if(isIn(fences[i],p))
{
if(fences[i].p1.X==fences[i].p2.X) // 垂直方向
{
ans+=fabs((double)p.X-fences[i].p1.X);
}
else
{
ans+=fabs((double)p.Y-fences[i].p1.Y);
}
}
else
{
ans+=min(dist_point(p,fences[i].p1),dist_point(p,fences[i].p2));
}
}
return ans;
} int main()
{
freopen("fence3.in","r",stdin);
freopen("fence3.out","w",stdout); cin>>n;
for(int i=;i<n;i++)
{
scanf("%lf %lf %lf %lf",&fences[i].p1.X,&fences[i].p1.Y,&fences[i].p2.X,&fences[i].p2.Y);
} Point p;
double d=1e100; for(int i=;i<=;i++)
for(int j=;j<=;j++)
{
if(d>dist(Point((double)i,(double)j)))
{
p=Point((double)i,(double)j);
d=dist(Point((double)i,(double)j));
}
} Point p2=p;
p2.X-=;
p2.Y-=;
Point ans;
for(;p2.X<=p.X+;p2.X+=0.1)
for(p2.Y=p.Y-;p2.Y<=p.Y+;p2.Y+=0.1)
{
if(d>=dist(p2)-1e-)
{
ans=p2;
d=dist(p2);
}
} printf("%.1f %.1f %.1f\n",ans.X,ans.Y,d); return ;
}
												

USACO6.4-Electric Fences:计算几何的更多相关文章

  1. 洛谷P2735 电网 Electric Fences

    P2735 电网 Electric Fences 11通过 28提交 题目提供者该用户不存在 标签USACO 难度普及/提高- 提交  讨论  题解 最新讨论 暂时没有讨论 题目描述 在本题中,格点是 ...

  2. USACO 6.4 Electric Fences

    Electric FencesKolstad & Schrijvers Farmer John has decided to construct electric fences. He has ...

  3. 洛谷 P2735 电网 Electric Fences Label:计算几何--皮克定理

    题目描述 在本题中,格点是指横纵坐标皆为整数的点. 为了圈养他的牛,农夫约翰(Farmer John)建造了一个三角形的电网.他从原点(0,0)牵出一根通电的电线,连接格点(n,m)(0<=n& ...

  4. LuoGu P2735 电网 Electric Fences

    题目传送门 这个东西,本来我是用求出两条一次函数解析式然后判断在x坐标下的y坐标值来做的 首先因为没考虑钝角三角形,WA了 然后又因为精度处理不好又WA了 一气之下,只能去网上查了查那个皮克定理 首先 ...

  5. luoguP2735 电网 Electric Fences

    一道校内模拟赛遇见的题 ** 不会正解就真的很麻烦的 数学题 ** 有一种东西叫 皮克定理 发现的千古神犇: 姓名:George Alexander Pick(所以叫皮克定理呀 国籍:奥地利(蛤!竟然 ...

  6. USACO 6.4 章节

    The Primes 题目大意 5*5矩阵,给定左上角 要所有行,列,从左向右看对角线为质数,没有前导零,且这些质数数位和相等(题目给和) 按字典序输出所有方案... 题解 看上去就是个 无脑暴搜 题 ...

  7. USACO 完结的一些感想

    其实日期没有那么近啦……只是我偶尔还点进去造成的,导致我没有每一章刷完的纪念日了 但是全刷完是今天啦 讲真,题很锻炼思维能力,USACO保持着一贯猎奇的题目描述,以及尽量不用高级算法就完成的题解……例 ...

  8. USACO6.5-Closed Fences:计算几何

    Closed Fences A closed fence in the plane is a set of non-crossing, connected line segments with N c ...

  9. USACO 6.5 Closed Fences

    Closed Fences A closed fence in the plane is a set of non-crossing, connected line segments with N c ...

随机推荐

  1. Cocos2D-X2.2.3学习笔记12(瞬间动作)

    到眼下我们已经学习了有 坐标系统 内存管理 UI系统 事件处理 几何图形 今天我们来学习动作管理OK 我们来看看类结构图 CCAction   全部动作的基类 以下派生了三个子类:CCFiniteTi ...

  2. java Serializable和Externalizable序列化反序列化详解--转

    一.什么是序列化? “对象序列化”(Object Serialization)是 Java1.1就开始有的特性. 简单地说,就是可以将一个对象(标志对象的类型)及其状态转换为字节码,保存起来(可以保存 ...

  3. Android(java)学习笔记258:JNI之hello.c(c代码功能实现)指针语法解析

    1. 接下来我们细讲分析一下前面一讲中,c功能实现的代码: (1)hello.c : #include <jni.h> char* getHello() { //////// return ...

  4. Android - 硬件抽象层(HAL)

    以下资料摘录整理自老罗的Android之旅博客,是对老罗的博客关于Android底层原理的一个抽象的知识概括总结(如有错误欢迎指出)(侵删):http://blog.csdn.net/luosheng ...

  5. MVC bundles

    Bundles用于打包CSS和javascript脚本文件,优化对它们的组织管理.显示模式则允许我们为不同的设备显示不同的视图. 减少请求数量和带宽,当然在开发调试时一般不开启.

  6. Windows Server 2008关闭internet explorer增强的安全配置

    服务器系统要求很高的安全性,所以微软给ie添加了安全增强.这就使得ie在Internet区域的安全级别一直是最高的,而且无法进行整体调整. 关闭IE SEC服务器系统要求很高的安全性,所以微软给ie添 ...

  7. iOS 网络与多线程--2.同步Get方式的网络请求(阻塞)

    通过Get请求方式同步获取网络数据.一旦发送同步请求,程序将停止用户交互,直至服务器返回数据. 之后在视图控制器文件(ViewController.m)内添加以下代码 在viewDidLoad函数内添 ...

  8. [<DDGuessYouLIkeModel 0x7c99faf0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key star.

    出现这个提示是由于以下原因造成: 这里我用到了MJExtension将字典转为模型,但再转为模型的时候,出现这个提示,原因就是因为NSInteger后面多一个一个“*” @property (nona ...

  9. PHP XML DOM

    PHP XML DOM 内建的 DOM 解析器使在 PHP 中处理 XML 文档成为可能. DOM 是什么? W3C DOM 提供了针对 HTML 和 XML 文档的标准对象集,以及用于访问和操作这些 ...

  10. 武汉科技大学ACM:1001: 猴子选大王

    Problem Description n只猴子要选大王,选举方法如下:所有猴子按 1,2 ……… n 编号并按照顺序围成一圈,从第 k 个猴子起,由1开始报数,报到m时,该猴子就跳出圈外,下一只猴子 ...